Rails controller redirect_to action with params without key
Rails controller redirect_to an action with params without key
我想在同一控制器中从一个动作重定向到另一个动作。
假设我有一个私有方法 my_params
returns 一个 ActionController::Parameters
实例。看起来像 { some_param: 'a', another_param: 'b' }
.
假设我想从 :action_1
从 :action_2
重定向到 params
。
当我尝试做 redirect_to :action, my_params
时,Rails 抱怨:
syntax error, unexpected end-of-input, expecting keyword_end
通常,我会这样写,
redirect_to :action, some_param: 'a', another_param: 'b'`
但在这种情况下我不能这样做,因为我想通过 my_params
。
有没有 Rails 方法来做到这一点?或者还有其他建议吗?
编辑
这不是重复项,因为我想传入一个已经是 ActionController::Parameters
.
实例的参数
您可以将当前的 my_params
参数散列传递给 redirect_to
方法,并且只更新 (merge
) 您想要更改的值。
redirect_to my_params.merge(action: :action_1)
我想在同一控制器中从一个动作重定向到另一个动作。
假设我有一个私有方法 my_params
returns 一个 ActionController::Parameters
实例。看起来像 { some_param: 'a', another_param: 'b' }
.
假设我想从 :action_1
从 :action_2
重定向到 params
。
当我尝试做 redirect_to :action, my_params
时,Rails 抱怨:
syntax error, unexpected end-of-input, expecting keyword_end
通常,我会这样写,
redirect_to :action, some_param: 'a', another_param: 'b'`
但在这种情况下我不能这样做,因为我想通过 my_params
。
有没有 Rails 方法来做到这一点?或者还有其他建议吗?
编辑
这不是重复项,因为我想传入一个已经是 ActionController::Parameters
.
您可以将当前的 my_params
参数散列传递给 redirect_to
方法,并且只更新 (merge
) 您想要更改的值。
redirect_to my_params.merge(action: :action_1)