开发者

Rails 3 issue with custom named resourceful route after update from rails 2

In the rails 2 version of my app I have this resource defined

map.resources :albums, :as => 'music', :has_many => :reviews

This gave me some standard routes:

album GET    /music/:id(.:format) {:controller=>"albums", :action=>"show"}
new_album GET    /music/new(.:format) {:controller=>"albums", :action=>"new"}

I also have a polymorphic association set up as follows:

class Album < ActiveRecord::Base
  has_man开发者_如何转开发y :reviews, :as => :reviewable
end


class Review < ActiveRecord::Base
  belongs_to :reviewable, :polymorphic => true
end

This meant I could create a link to my album resource through my review by using a polymorphic path

<%= review.reviewable.title, polymorphic_path(review.reviewable) %>

However, since upgrading to rails 3 I've changed my routes file to in order to comply with the new router:

resources :music, :controller => 'albums' do
  resources :reviews
end

And although cleaner it breaks my polymorphic link, i.e.

<%= review.reviewable.title, polymorphic_path(review.reviewable) %>

I end up with the following error message

undefined method `album_path' 

If I look in my rails 3 generated routes with "rake routes" i see

music GET    /music/:id(.:format) {:action=>"show", :controller=>"albums"}

which is different to what was there before in the rails 2, i.e.

album GET    /music/:id(.:format) {:controller=>"albums", :action=>"show"}

So my guess is that this is what's breaking things.

I'm a little unsure though as how to fix it without reverting back to the rails 2 routes which will be depreciated with rails 3.1. Any help would be appreciated. Thanks.


It turns out this was easy to fix. All I had to do was:

resources :albums, :path => 'music' do
  resources :reviews
end
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜