Restful Routes not working!

I've spent too many hours trying to figure out why this won't work...

In my routes.rb, I have:

map.resources :auctions, :has_many => :bids

I've also tried

map.resources :auctions do |auction| auction.resources :bids end

When I try and use the bids paths like bid_path or bids_path, rails throws and error and tells me that bid_path or bids_path is an undefined symbol/method. So it seems to not know about the route or anything...I've checked over and it seems the syntax is correct which is why I'm stumped. Any insights?

Hi --

I've spent too many hours trying to figure out why this won't work...

In my routes.rb, I have:

map.resources :auctions, :has_many => :bids

I've also tried

map.resources :auctions do |auction| auction.resources :bids end

When I try and use the bids paths like bid_path or bids_path, rails throws and error and tells me that bid_path or bids_path is an undefined symbol/method. So it seems to not know about the route or anything...I've checked over and it seems the syntax is correct which is why I'm stumped. Any insights?

You want auction_bid_path and auction_bids_path. The nesting is expressed in the named routes. Also, you have to stack objects when you use them:

   <%= link_to "Click", auction_bid_path(@auction, @bid) %>

etc. (Unless you want to try my rather experimental Inferred Routes plugin, which lets you just use (@bid). See: http://www.risleydale.net/svn/plugins/inferred_routes.)

David

are you on rails 2.0? :slight_smile: If so, check your routes with rake task:

rake routes

make sure that some scaffold didn't overwrite your config routes (last occurence matters)

That way seems to do it, at least now it sees that the paths are defined. Thanks!

Just wondering, in the tutorials I used it just showed using the paths as bid_path and bids_path. Was this changed in recent versions, or has it always been this way?

Hi --

That way seems to do it, at least now it sees that the paths are defined. Thanks!

Just wondering, in the tutorials I used it just showed using the paths as bid_path and bids_path. Was this changed in recent versions, or has it always been this way?

It's changed. A common problem used to arise if you wanted both nested and non-nested routes for a given resource:

   map.resources :bids    map.resources :auctions do |a|      a.resources :bids    end

This tended to give the impression that you could do both this:

   bid_path(@bid)

and this:

   bid_path(@auction,@bid)

but you couldn't, because map.resources is basically a macro that writes methods for you and if you write the bid_path method twice, only the second version will be usable.

The solution was to add :name_prefix => "auction_" to the second one. More recently, the addition of the name prefix became automatic, and it happens whether or not there's also a separate bids resource.

David

Oh I see, thanks a lot! And yes, I am running Rails 2.1 right now.

Do you guys happen to know of any up to date Restful tutorials? :slight_smile: All the ones I have seem to be outdated since they refer to the same thing which doesn't work right now.

Just wondering, in the tutorials I used it just showed using the paths as bid_path and bids_path. Was this changed in recent versions, or has it always been this way?

You're using nested routes, and they always worked this way.

David mentioned solutions to this above, but personally I think that it's better to use traditional approach. Especially when used in views. Such routes as: new_auction_bid_path(@auction) are more informative. Common pattern is to use nested resources only in relation parent -> child, so using some experimental plugins isn't best choice in my opinion.

look at this example:

map.resources :articles, :has_many => :comments map.resources :authors, :has_many => :articles

what will be: article_path(@article) using this plugin? /authors/5/articles/4 or /articles/5?

End the end I will repeat: Checking 'rake routes' solves most problems when problems with routing occurs....

Hi --

Just wondering, in the tutorials I used it just showed using the paths as bid_path and bids_path. Was this changed in recent versions, or has it always been this way?

You're using nested routes, and they always worked this way.

No -- see my earlier email. Here's an example, using Rails 1.2.3.

In routes.rb:

   map.resources :auctions do |a|      a.resources :bids    end

In the console:

r = ActionController::Routing::Routes

=> [lots of output]

puts r.named_routes.map {|x| x[0].to_s }.grep(/bid/).sort

bid bids edit_bid formatted_bid formatted_bids formatted_edit_bid formatted_new_bid new_bid

r.recognize_path(app.bids_path(:auction_id => 1), :method => :get)

=> {:controller=>"bids", :action=>"index", :auction_id=>"1"}

r.recognize_path(app.auction_bids_path(:auction_id => 1), :method => :get)

NoMethodError: undefined method `auction_bids_path' for #<ActionController::Integration::Session:0x20e66ac>

It may be ancient history (except for the concern about outdated tutorials), but it really did once work that way.

David mentioned solutions to this above, but personally I think that it's better to use traditional approach. Especially when used in views. Such routes as: new_auction_bid_path(@auction) are more informative. Common pattern is to use nested resources only in relation parent -> child, so using some experimental plugins isn't best choice in my opinion.

I don't think using an experimental plugin in production is ever a good idea, but I thought it was worth mentioning since we were discussing the problem space it addresses.

look at this example:

map.resources :articles, :has_many => :comments map.resources :authors, :has_many => :articles

what will be: article_path(@article) using this plugin? /authors/5/articles/4 or /articles/5?

article_path has nothing to do with author_article_path. The plugin sees no more ambiguity in this than the routing system in general does.

The point of the plugin is that this:

   author_article_path(@article)

will be interpreted by the plugin as if it were:

   author_article_path(@article.author, @article)

In other words, it infers the route segments from the one object.

David

I don't think using an experimental plugin in production is ever a good idea, but I thought it was worth mentioning since we were discussing the problem space it addresses.

Of course it was worth mentioning. I'll give it a try. As I remember 'make_resourceful' has similiar features, but it goes even further: you can use just: object_path(@object). What's cool It is aware of current namespace

The point of the plugin is that this:

   author_article_path(@article)

will be interpreted by the plugin as if it were:

   author_article_path(@article.author, @article)

Ok, Thanks for explanation. I should have checked this plugin more carefully. IMO there is very little advantage of using this... (in comparision to make_resourceful) I know that these plugin aren't equivalent.

Ok, now I'm trying to create a new link for a bid. Doing this gives an error:

<%= link_to 'New bid', new_auction_bid_path(@auction) %>

It says there is no method bid_path....seriously, I have to admit I'm getting frustrated with all these routes and stuff. I've looked all over for updated tutorials and have found nothing so I'm confused as to how people learn this.

Mike C wrote:

Ok, now I'm trying to create a new link for a bid. Doing this gives an error:

<%= link_to 'New bid', new_auction_bid_path(@auction) %>

It says there is no method bid_path....seriously, I have to admit I'm getting frustrated with all these routes and stuff. I've looked all over for updated tutorials and have found nothing so I'm confused as to how people learn this. >

Can you show us the exact error please?