具有多个标识符的铁路路线?
我正在尝试在 Rails 3 中设置如下所示的路由:
/items/:category/:name/
进行匹配来设置它非常容易,然后使用以下内容生成 URL:
item_path(:category => @item.category, :name => @item.name)
但是有什么方法可以设置它以便 item_path @item并且 form_for @item 会自动工作,所以我不必每次都传递类别?
谢谢!
I'm trying to set up routes in Rails 3 that look like:
/items/:category/:name/
It's pretty easy to do a match to set this up, and then generate the URL with the following:
item_path(:category => @item.category, :name => @item.name)
But is there any way to set it up so that item_path @item and form_for @item will work automatically, so I don't have to pass the category every time?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不是真的没有。我建议在 Item 上定义一个 to_params (注意“s”)方法,如下所示
:
def to_params
{:类别=>类别,:名称=>姓名}
结尾
然后像
item_path(@item.to_params)
那样调用它。如果你将一些东西修改为默认值,我可以保证你会遇到你不想要的情况。Not really no. I would suggest defining a to_params (note the 's') method on Item as follows:
def to_params
{:category => category, :name => name}
end
And then calling it like so
item_path(@item.to_params)
. If you hack things to default to this I can guarantee you'll run into situations in which you don't want it.