Search code examples
ruby-on-railsforms

forms in ruby on rails, passing variables between one form to another


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.


Solution

  • 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.

    1. User enters http://localhost:3000/products into their address bar to initiate a GET request.

    2. Rails routes this request to the index action of products controller.

    3. Rails will execute ProductsController#index (the index method located in app/controllers/products_controller.rb)

    4. 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 %>
      
    5. User fills in the SearchInput field and submits the form as a POST request to http://localhost:3000/products/search

    6. Rails routes this request to the search action of the products controller.

    7. 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
      
    8. 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 %>