如何在redirect_to中传递参数? 我知道我们可以用这个传递id:
redirect_to :action => action_name,:id => 3
如果我想传递额外的参数,比如一些表单数据,如何实现呢?
编辑:
对于Ruby 2语法,你必须将上面的代码片段更新为:
redirect_to action: action_name, id: 3
如何在redirect_to中传递参数? 我知道我们可以用这个传递id:
redirect_to :action => action_name,:id => 3
如果我想传递额外的参数,比如一些表单数据,如何实现呢?
编辑:
对于Ruby 2语法,你必须将上面的代码片段更新为:
redirect_to action: action_name, id: 3
当前回答
如果你正在寻找一种方法来传递额外的URL参数(不是控制器,动作,id等),这里有一个健壮的方法来这样做:
object_path(@object, params: request.query_parameters)
这将传递utm参数或您不想丢失的任何其他附加参数。
其他回答
如果您有一些表单数据,例如发送到home#action,现在您希望在保留参数的同时将它们重定向到house#act,您可以这样做
redirect_to act_house_path (request.parameters)
redirect_to :controller => "controller_name", :action => "action_name", :id => x.id
routes.rb
match 'controller_name/action_name' => 'controller_name#action_name', via: [:get, :post], :as => :abc
任何你想重定向参数的控制器如下所示:
redirect_to abc_path(@abc, id: @id), :notice => "message fine"
如果您正在使用RESTful资源,您可以执行以下操作:
redirect_to action_name_resource_path(resource_object, param_1: 'value_1', param_2: 'value_2')
or
#You can also use the object_id instead of the object
redirect_to action_name_resource_path(resource_object_id, param_1: 'value_1', param_2: 'value_2')
or
#if its a collection action like index, you can omit the id as follows
redirect_to action_name_resource_path(param_1: 'value_1', param_2: 'value_2')
#An example with nested resource is as follows:
redirect_to edit_user_project_path(@user, @project, param_1: 'value_1', param_2: 'value_2')
你要定你的路,取巴仑回来。
redirect_to controller: "client", action: "get_name", params: request.query_parameters and return