简体   繁体   中英

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.

I guess this use case is pretty common, but rails for some reason not support it or i missed something ?

You just pass params as additional keys:

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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