问题
I have routes defined like
scope ':cityname' do
resources comments
end
Note cityname = comment.user.cityname
so url helpers such as comment_path(@comment)
can generate links like
/newyork/comments/1
/boston/comments/2
/miami/comments/3
How to set this :cityname
url option based on the model attribute?
I found a related question here : default_url_options and rails 3
Thanks!
回答1:
You can try something like this
class ApplicationController < ActionController::Base
def url_options
{ :cityname => @comment.user.cityname }.merge(super)
end
end
class YourController < ...
def calledaction
@comment = Comment.find(1)
end
end
来源:https://stackoverflow.com/questions/18756553/rails-url-options-based-on-model-attribute