Reputation: 2065
I'm attempting to pass variables between forms and unfortunately I'm not managing to.
In my index.html.erb
, I have a text field named SearchInput
and I would like to pass this variable to the next form search.html.erb
, so that I may populate some data accordingly. How may I go about this, please?
In my index.html.erb
, I have the following:
<% form_tag (:action => 'search') do %>
I'm really clueless on how to go about on this please, any tips would be really appreciated.
Upvotes: 3
Views: 22411
Reputation: 536
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:
Say you have a variable called @foo that you want to pass to your new controller action, and your new view contains a form for your user to fill out. In which case, you can use
<%= 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.
Clicking on Link Text will direct Rails to the new action of your WidgetController. You can have
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].
Then, in your new.html.erb view file, you can allow the user to create a new Widget object with this pre-defined foo attribute already filled out via a hidden form field:
<%= 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 foo attribute already filled out!
Upvotes: 0
Reputation: 23450
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
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.
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.
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.
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/products into 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"])
end
Unless 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 %>
Upvotes: 29
Reputation: 33171
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.
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
Upvotes: 2