开发者

How do I use redirect_to() with everything in the current params[] array in rails 3?

I would like to send the redirect_to function all of the current page's parameters without knowing the individual params. The parameters on a particular page are variable in quantity and are not always known during development. So far I have only found examples of sending known params and values.

I want to do this:

redirect_to named_path, :notice => 'Some notice.', :params => params

I don't want to do t开发者_开发知识库his:

redirect_to named_path, :notice => 'Some notice.', :param1 => "v1", :param2 => "v2", ...

TIA!


This could work: (not tested it)

redirect_to named_path, params.merge({ :notice => 'Some notice' })

The idea is to make the last parameter of redirect_to a single hash based on the original params hash and merge in any additional value.


You can use

redirect_to named_path(params.merge(:notice => 'Some notice.'))

you can even change parameters, delete some by using params.merge() with his normal behavior :)


You can add more params to current path with:

redirect_to request.params.merge({new_params: 'foo'})

Further, you can pass all of the current params with a new param to a new path with this code:

redirect_to new_path(request.params.merge({new_params: 'foo'}).except(:action, :controller))

params is an instance of ActionController::Parameters. So it can't be converted to hash and doesn't support .merge method. Thus, use request.params instead.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜