简体   繁体   English

Rails,为任意控制器,动作和零个或多个参数构造URL

[英]Rails, construct url for arbitrary controller,action and zero or more params

here is my current workflow: 这是我当前的工作流程:

c = get_some_controller # => "fruits" or nil
a = get_some_action # => "list"
p = get_some_params # => { :type => :orange, :price => 10 }

url = url_for(:controller => c, :action => a) # no option for params ??

So, here is my question, url_for seem does not have any option to pass params. 所以,这是我的问题, url_for似乎没有任何传递参数的选项。

I guess this use case is pretty common, but rails for some reason not support it or i missed something ? 我猜这个用例很普遍,但是Rails由于某种原因不支持它,或者我错过了一些东西?

You just pass params as additional keys: 您只需将params作为附加键传递:

url_for(p.merge(:controller => c, :action => a))

Or to perhaps be more clear, you could also have done: 或者更明确地说,您也可以这样做:

url_for(:controller => c, :action => a, :type => :orange, :price => 10)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM