ruby on rails 中的表单,将变量在一种形式之间传递到另一种形式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1780665/
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
forms in ruby on rails, passing variables between one form to another
提问by Erika
I'm attempting to pass variables between forms and unfortunately i'm not managing
我试图在表单之间传递变量,不幸的是我没有管理
in my index.html.erb, i have a text field named SearchInputand i would like to pass this variable to the next form search.html.erbso that i may populate some data accordingly. How may i go about this please?
在 my 中index.html.erb,我有一个名为的文本字段SearchInput,我想将此变量传递给下一个表单,search.html.erb以便我可以相应地填充一些数据。请问我该怎么做?
In my index.html.erbi have the following
<% form_tag (:action => 'search') do %>
在我的index.html.erb我有以下
<% form_tag (:action => 'search') do %>
I'm really clueless on how to go about on this please, any tips would be really appreciated
我真的对如何去做这件事一无所知,任何提示将不胜感激
回答by EmFi
It sounds like you could benefit from reading up on the interactions between Model, View and Controller in a MVC framework like Rails. I suggest the official Getting Started Guide
听起来您可以从阅读 Rails 等 MVC 框架中模型、视图和控制器之间的交互中受益。我建议使用官方入门指南
Here's the relevant details as it relates to your question:
以下是与您的问题相关的相关详细信息:
When receiving a HTTP request Rails will route the request to a controller action based on the url path. The controller than builds the appropriate response by evaluating view templates in the context of the request. The standard flow in Rails is the controller will extract information from the params hash to query the database for the information required to construct the view. Instance values set in the controller (variables with names starting with '@') will be available to the views rendered by this controller during this action.
当接收到 HTTP 请求时,Rails 会根据 url 路径将请求路由到控制器操作。然后控制器通过在请求的上下文中评估视图模板来构建适当的响应。Rails 中的标准流程是控制器将从 params 哈希中提取信息以查询数据库以获取构建视图所需的信息。在此操作期间,控制器中设置的实例值(名称以“@”开头的变量)将可用于此控制器呈现的视图。
Forms in general, direct the client's web browser to send a POST request to the specified url when the submit button is clicked. This request contains the form's fields and their values as data in the header.
表单通常会在单击提交按钮时指示客户端的 Web 浏览器向指定的 url 发送 POST 请求。此请求包含表单的字段及其值作为标题中的数据。
Rails interprets the form data and any other parameters in the params hash. Which you can access in the controller or view of any action. The generally accepted place to access the params hash is in the controller.
Rails 解释表单数据和 params 散列中的任何其他参数。您可以在控制器或任何操作的视图中访问。访问 params 散列的普遍接受的地方是在控制器中。
Lets walk through the process of what's actually happening. The following is in terms of running a server in development assuming we're talking about a controller for products, and you've set up routes correctly.
让我们来看看实际发生的过程。以下是关于在开发中运行服务器的情况,假设我们正在讨论产品的控制器,并且您已经正确设置了路由。
User enters http://localhost:3000/productsinto their address bar to initiate a GET request.
Rails routes this request to the index action of products controller.
Rails will execute ProductsController#index (the index method located in app/controllers/products_controller.rb)
Unless you explicitly render or redirect the request, Rails will render the products/index template (app/views/products/index.html.erb) which contains a small form that submits to the search action of ProductsController. Form code will look like this:
<% form_tag(:action => 'search') do %> <%= text_field_tag "SearchInput" %> <%= submit_tag %> <% end %>User fills in the SearchInput field and submits the form as a POST request to http://localhost:3000/products/search
Rails routes this request to the search action of the products controller.
Rails executes ProductsController#search, the params hash contains the value the user entered into the SearchInput field with the SearchInput key. Here's a very simple example of using the form input to perform a search.
def search @products = Product.find_by_name(params["SearchInput"]) endUnless you explicitly render or redirect the request, Rails will render the products/search template (app/views/products/search.html.erb) Example search view:
<% if @products.empty? %> No products found <% else %> <% @products.each do |product| %> <%= link_to product.name, product_url(product)%> <br /> <% end %> <% end %>
用户在地址栏中输入http://localhost:3000/products以发起 GET 请求。
Rails 将此请求路由到 products 控制器的 index 操作。
Rails 将执行 ProductsController#index(位于 app/controllers/products_controller.rb 中的 index 方法)
除非您显式地呈现或重定向请求,否则Rails 将呈现products/index 模板(app/views/products/index.html.erb),其中包含一个提交给ProductsController 搜索操作的小表单。表单代码如下所示:
<% form_tag(:action => 'search') do %> <%= text_field_tag "SearchInput" %> <%= submit_tag %> <% end %>用户填写 SearchInput 字段并将表单作为 POST 请求提交到http://localhost:3000/products/search
Rails 将此请求路由到产品控制器的搜索操作。
Rails 执行 ProductsController#search,params 散列包含用户使用 SearchInput 键在 SearchInput 字段中输入的值。这是使用表单输入执行搜索的一个非常简单的示例。
def search @products = Product.find_by_name(params["SearchInput"]) end除非您显式地呈现或重定向请求,否则 Rails 将呈现 products/search 模板 (app/views/products/search.html.erb) 示例搜索视图:
<% if @products.empty? %> No products found <% else %> <% @products.each do |product| %> <%= link_to product.name, product_url(product)%> <br /> <% end %> <% end %>
回答by nowk
Form data is passed as part of the params[..]hash. So you can access most of the data passed through a form in that manner.
表单数据作为params[..]散列的一部分传递。因此,您可以以这种方式访问通过表单传递的大部分数据。
In your case, your view
在你的情况下,你的观点
<form action="..." method="...">
<input type=".." name="search_input" ... />
...
...
<button>submit</button>
</form>
would be grabbed via, your controller
将通过,您的控制器抓取
def action_name
@collection = Model.find(:conditions => {:column_name => params['search_input']})
...
...
end
回答by Amy.js
Here's how you can pass a parameter around via the link_to method in order to, say, go to another form in order to create a new object with the passed parameter. This strategy would allow you to pass variables among actions in your controller and create objects with predefined attributes:
下面是如何通过 link_to 方法传递参数,以便转到另一个表单,以便使用传递的参数创建新对象。此策略将允许您在控制器中的操作之间传递变量并创建具有预定义属性的对象:
Say you have a variable called @foothat you want to pass to your newcontroller action, and your newview contains a form for your user to fill out. In which case, you can use
假设您有一个名为@foo的变量,您希望将其传递给新的控制器操作,并且您的新视图包含一个供用户填写的表单。在这种情况下,您可以使用
<%= link_to "Link Text", new_widget_path(:foo => @foo) %>
which would store @foo in params[:foo], allowing you to use params[:foo] in your controller.
它会将@foo 存储在 params[:foo] 中,允许您在控制器中使用 params[:foo]。
Clicking on Link Textwill direct Rails to the newaction of your WidgetController. You can have
单击链接文本会将 Rails 定向到WidgetController的新操作。你可以有
def new
@widget = Widget.new(:foo => params[:foo])
end
which would define a new Widget object with the :foo attribute pre-filled with params[:foo].
这将定义一个新的 Widget 对象,其中 :foo 属性预先填充了 params[:foo]。
Then, in your new.html.erbview file, you can allow the user to create a new Widget object with this pre-defined fooattribute already filled out via a hidden form field:
然后,在您的new.html.erb视图文件中,您可以允许用户创建一个新的 Widget 对象,该对象已通过隐藏表单字段填写了此预定义的foo属性:
<%= form_for(@widget) do |f| %>
<%= f.label :other_attribute %><br />
<%= f.text_field :other_attribute %>
<%= f.hidden_field :foo %>
<%= f.submit %>
<% end %>
Allowing the user to create a new Widget object with a custom other_attribute and the fooattribute already filled out!
允许用户使用自定义的 other_attribute 和已经填写的foo属性创建一个新的 Widget 对象!

