【问题标题】:Rails - Member Action with Parameter - path helper?Rails - 带参数的成员操作 - 路径助手?
【发布时间】:2011-09-12 03:12:23
【问题描述】:
我看到了这篇关于向成员动作路由添加参数的帖子:
Rails3 Routes - Passing parameter to a member route
当我手动输入完整的 URL 时,路由效果很好:www.whatever.com/resource/:resource_id/action/:action_parameter
是否有一个方便的路径助手可以用来在我的视图中链接到这个?例如,如果只是普通的成员动作,没有参数,我可以使用
action_resource_path(:resource_id)
是否有与额外参数等效的?
【问题讨论】:
标签:
ruby-on-rails
ruby
routing
helper
view-helpers
【解决方案1】:
您可以使用 as 选项指定 url 助手:
resources :foos do
collection do
get 'bar/:resource_id', :action => :bar, :as => 'bar'
end
end
生成的路由如下所示:
$ rake routes | grep bar
bar_foos GET /foos/bar/:resource_id(.:format) {:action=>"bar", :controller=>"foos"}
你可以这样使用:
bar_foos_path(:resource_id => @resource.id)