在rails redirect_to中传递参数
我们如何在rails中传递参数redirect_to? 我知道我们可以通过使用这个ID:
redirect_to :action => action_name,:id => 3
如果我想传递额外的参数,如某些表单数据如何实现呢?
编辑:
对于Ruby 2语法,您必须将上面的代码片段更新为:
redirect_to action: action_name, id: 3
只需将它们附加到选项:
redirect_to controller: 'thing', action: 'edit', id: 3, something: 'else'
会产生/thing/3/edit?something=else
如果您使用的是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他们的房子#行为,同时保持参数,你可以做到这一点
redirect_to act_house_path(request.parameters)
您可以使用flash参数将任意对象传递给模板。
redirect_to :back, flash: {new_solution_errors: solution.errors}
然后通过散列在模板中访问它们。
<% flash[:new_solution_errors].each do |err| %>
redirect_to new_user_path(:id => 1, :contact_id => 3, :name => 'suleman')
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的任何控制器如下:
redirect_to abc_path(@abc, id: @id), :notice => "message fine"