Ruby-on-rails 在 rails redirect_to 中传递参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1430247/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 21:45:39  来源:igfitidea点击:

Passing parameters in rails redirect_to

ruby-on-rails

提问by markiv

How do we pass parameters in redirect_to in rails? I know we can pass id using this:

我们如何在rails中的redirect_to中传递参数?我知道我们可以使用这个传递 id:

redirect_to :action => action_name,:id => 3

If I want to pass additional parameters like some form data how to achieve it?

如果我想传递额外的参数,比如一些表单数据,如何实现呢?

EDIT:

编辑:

For Ruby 2 syntax you have to update the snippet above to:

对于 Ruby 2 语法,您必须将上面的代码段更新为:

redirect_to action: action_name, id: 3

回答by Michael Sepcot

Just append them to the options:

只需将它们附加到选项:

redirect_to controller: 'thing', action: 'edit', id: 3, something: 'else'

Would yield /thing/3/edit?something=else

会屈服 /thing/3/edit?something=else

回答by Sohan

If you are using RESTful resources you can do the following:

如果您使用 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')

回答by v4r

If you have some form data for example sent to home#action, now you want to redirect them to house#act while keeping the parameters, you can do this

如果您有一些表单数据,例如发送到 home#action,现在您想将它们重定向到 house#act,同时保留参数,您可以这样做

redirect_to act_house_path(request.parameters)

redirect_to act_house_path(request.parameters)

回答by vermasque

You can pass arbitrary objects to the template with the flash parameter.

您可以使用 flash 参数将任意对象传递给模板。

 redirect_to :back, flash: {new_solution_errors: solution.errors}

And then access them in the template via the hash.

然后通过哈希在模板中访问它们。

<% flash[:new_solution_errors].each do |err| %>

回答by Muhammad Suleman

redirect_to new_user_path(:id => 1, :contact_id => 3, :name => 'suleman')

回答by Karwin

redirect_to :controller => "controller_name", :action => "action_name", :id => x.id

回答by Abram

If you are looking for a way to pass additional URL parameters (not controller, action, id, etc), here's a robust method for doing so:

如果您正在寻找一种传递额外 URL 参数(不是控制器、操作、id 等)的方法,这里有一个强大的方法:

object_path(@object, params: request.query_parameters)

That will pass along utm parametersor any other additional params you don't want to lose.

这将传递utm 参数或您不想丢失的任何其他附加参数。

回答by Mitesh Sharma

routes.rb

路由文件

 match 'controller_name/action_name' => 'controller_name#action_name', via: [:get, :post], :as => :abc

Any controller you want to redirect with parameters are given below:

您要使用参数重定向的任何控制器如下所示:

redirect_to abc_path(@abc, id: @id), :notice => "message fine" 

回答by Darlan Dieterich

Route your path, and take the params, and return:

路由你的路径,然后走params,然后返回:

redirect_to controller: "client", action: "get_name", params: request.query_parameters and return