开发者

In Rails 3.0, is there a way for UrlHelper's path method output a nested-resource's route without explicitly specifying the parent?

Let's say I have this setup: An Area has many :places

The routes.rb is something like:

resources :areas, :path=>"/" do    
  resources :places, :path=>'/places' 
end

And so let's say I have the area "Midw开发者_高级运维est" and the place "Chicago"...to get to the Chicago place page, I do:

area_place_path(place, place.area)

I would really prefer a method that would be able to guess the place's area (since it's a many-to-one relationship) without having to explicitly pass it in as an argument. I know how to write one custom for my specific needs, but I was wondering if there was a magic Rails way that I'm ignorant of?


It seems like there should be a magic Rails way but there isn't. Such a feature can be added in theory but if you read the related Rails code you'll find that such a feature would be a violation of separation of concerns (UrlHelper is part of ActionView but this functionality would need to be aware of ActiveRecord associations) so I don't think this feature will be added any time soon if ever. I personally don't see any harm in writing your own path helpers and a path helper to give you this functionality would be trivial to write. My suggestion is to just write your own path helper. There really should be an easier way (a plugin perhaps?).

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜