Rails url options based on model attribute

岁酱吖の 提交于 2019-12-11 19:33:44

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!