简体   繁体   English

Rails 控制器重定向到一个没有键参数的动作

[英]Rails controller redirect_to an action with params without key

I would like to redirect from one action to another in a same controller.我想在同一个控制器中从一个动作重定向到另一个动作。

Suppose I have a private method my_params that returns an ActionController::Parameters instance.假设我有一个私有方法my_params返回一个ActionController::Parameters实例。 It looks like { some_param: 'a', another_param: 'b' } .它看起来像{ some_param: 'a', another_param: 'b' }

Let's say I want to redirect from :action_1 from :action_2 passing params .假设我想从:action_1重定向到:action_2传递params

When I try to do redirect_to :action, my_params , Rails complains:当我尝试做redirect_to :action, my_params ,Rails 抱怨:

syntax error, unexpected end-of-input, expecting keyword_end

Normally, I would write something like,通常,我会写一些类似的东西,

redirect_to :action, some_param: 'a', another_param: 'b'`

but I cannot do so in this case, because I want to pass my_params .但在这种情况下我不能这样做,因为我想通过my_params

Is there a Rails way to do this?有没有 Rails 的方法来做到这一点? Or any other suggestions?或者有什么其他建议?

Edit编辑

This is not a duplicate because I want to pass in a param that is already an instance of ActionController::Parameters .这不是重复的,因为我想传入一个已经是ActionController::Parameters实例的ActionController::Parameters

您可以将当前的my_params参数散列传递给redirect_to方法,并且只更新( merge )您想要更改的值。

redirect_to my_params.merge(action: :action_1)

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

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