基于模型属性的 Rails url 选项 [英] Rails url options based on model attribute
本文介绍了基于模型属性的 Rails url 选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我定义了类似的路由
scope ':cityname' 做资源评论结尾
注意 cityname = comment.user.cityname
所以像 comment_path(@comment)
这样的 url helpers 可以生成像
/newyork/comments/1/波士顿/评论/2/迈阿密/评论/3
如何根据模型属性设置这个 :cityname
url 选项?
我在这里找到了一个相关的问题:default_url_options 和 rails 3 >
谢谢!
解决方案
你可以试试这样的
class ApplicationController <动作控制器::基础def url_options{ :cityname =>@comment.user.cityname }.merge(super)结尾结尾类 YourController <...默认调用操作@comment = Comment.find(1)结尾结尾
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!
解决方案
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
这篇关于基于模型属性的 Rails url 选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文