Liad
Liad

Reputation: 33

Strange behavior of Ruby on Rails Routing

I am a Ruby newbie and I have been struggling with the following routing problem:

I have a Publishers controller in my app, and I want to use it as a singular resource, so a logged-in publisher could go to his profile using the /publisher route.

My routes file has the following

resources :publishers, :only => [:index, :show, :edit, :update]
resource :publisher, :only => [:show, :edit, :update]

And in my view controller I have the link

<li><%= link_to "View General Settings", publishers_path(@publisher) %></li>

The problem: the links is translated to URL /publisher.1 (instead of publisher/1)

(.1 represents publisher_id; when the publisher_id is 2, it renders to /publisher.2)

Any suggestions as to why this happens, and how to fix it?

I post here my routes:

    publishers GET /publishers(.:format)          {:action=>"index", :controller=>"publishers"}
edit_publisher GET /publishers/:id/edit(.:format) {:action=>"edit", :controller=>"publishers"}
     publisher GET  /publishers/:id(.:format)     {:action=>"show", :controller=>"publishers"}
               PUT  /publishers/:id(.:format)     {:action=>"update", :controller=>"publishers"}
               POST /publisher(.:format)          {:action=>"create", :controller=>"publishers"}
 new_publisher GET  /publisher/new(.:format)      {:action=>"new", :controller=>"publishers"}
               GET  /publisher/edit(.:format)     {:action=>"edit", :controller=>"publishers"}
               GET  /publisher(.:format)          {:action=>"show", :controller=>"publishers"}
               PUT  /publisher(.:format)          {:action=>"update", :controller=>"publishers"}

Thanks!

Upvotes: 3

Views: 395

Answers (2)

Srdjan Pejic
Srdjan Pejic

Reputation: 8202

Firstly, as Tyler said, you're using the publishers resource routes as opposed to publisher ones.

Since this is going to cause confusion in the future, I'd suggest removing the singular resource publisher and matching the named route to the show action of the publishers controller, like this:

match "/publisher/:id" => "publishers#show"

Also, if you don't want to have the ID part in the named route, you can remove it, but then you'll have to come up with a way of storing the ID of a publisher in some way. A session variable comes to mind.

Hope this helps

Upvotes: 1

Tyler Eaves
Tyler Eaves

Reputation: 13121

Try using publisher_path rather than publishers_path?

Upvotes: 1

Related Questions