Rails - 带参数的成员操作 - 路径助手?

时间:2011-09-12 03:12:23

标签: ruby-on-rails ruby routing helper view-helpers

我看到这篇关于向成员行动路线添加参数的帖子:

Rails3 Routes - Passing parameter to a member route

当我手动输入完整的URL时,路由工作正常:www.whatever.com/resource/:resource_id/action/:action_parameter

我可以使用方便的路径助手在我的视图中链接到此吗?例如,如果它只是一个没有参数的普通成员动作,我可以使用

action_resource_path(:RESOURCE_ID)

是否有额外参数的等效物?

1 个答案:

答案 0 :(得分:4)

您可以使用as选项指定网址助手:

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)