Bhav
Bhav

Reputation: 2185

Pass selected value to Rails controller

I've got a method named 'statement' in a balances controller (balances_controller.rb) that returns information from a specific date (@latestDate) to a view (statement.html.rb)

@latestDate = the most recent date in the 'date' column in the 'balances' table

In the view, I've also got a dropdown list of all the dates and when a date is selected, I'd like to be able to update the information so that in effect, the statement method is called again but the @latestDate is set to the selected value from the dropdown list.

How can I do this?

UPDATE:

I've modified my code with brito's suggestions. When I press the Search button, the form passes the following params:

?utf8=✓&date_id=30%2F12%2F2015&commit=Search 

However, if I select a date and click Search, the @latestDate gets set and the h1 tag gets displayed correctly, but the rest of the data doesn't get returned.

CODE:

balances_controller.rb

  ....
  def statement
    @dates = Balance.select("distinct(date)").order('date desc')

    #Updated
    if (params[:date_id].blank?)
      @latestDate = Balance.order('date desc').first.date
    else
      @latestDate = params[:date_id]
    end

    @summaryBalances = Balance.joins(:account).order('accounts.credit').where('date = :abc', {abc: @latestDate})
  end
  ....

balances/statement.html.rb

....
<h1>Statement at <%= @latestDate %></h1>

#UPDATED
<%= form_tag("/statement", method: "get") do %>
<%= select_tag "date_id", options_for_select( @dates.collect {|d| [ d.date, d.date ] }) %>
<%= submit_tag("Search") %>
<% end %>

<div class="table-responsive">
<table class="table">
  <thead>
    <tr>
      <th>Account</th>
      <th>Balance</th>
      <th colspan="1"></th>
    </tr>
  </thead>

  <tbody>
    <% @summaryBalances.each do |balance| %>
      <tr>
        <td><%= balance.account.name %></td>    
        <td class='account-balance'><%= number_to_currency(balance.balance, unit: "£") %></td>
        <td><%= link_to 'Edit', edit_balance_path(balance) %></td>
      </tr>
    <% end %>
  </tbody>
</table>
</div>
....

Upvotes: 0

Views: 3865

Answers (1)

Leo Brito
Leo Brito

Reputation: 2051

A "dropdown" is a kind of input (HTML <select>). <input>s and <select>s must be within a <form> HTML element.

Using pure HTML

Whatever is in the <form> will be accessible in the params hash. So if you have a dropdown list, it should be within a <form> like this:

<form accept-charset="UTF-8" action="/yourroute" method="get">
  <select name="car">
    <option value="volvo">Volvo</option>
    <option value="saab">Saab</option>
    <option value="opel">Opel</option>
    <option value="audi">Audi</option>
  </select>
</form>

In the example above, the selected value (e.g. "volvo") will be available in the controller within params["car"]. Note that you would still have to define /yourroute.

Using Rails' helper methods

Of course, Rails helps you generate forms and inputs. You can use Rails' form helpers for that. For example, the following generates a search form ("Search" label, text box for input and a button) that sends a GET to the /search route:

<%= form_tag("/search", method: "get") do %>
  <%= label_tag(:q, "Search for:") %>
  <%= text_field_tag(:q) %>
  <%= submit_tag("Search") %>
<% end %>

EDIT

To check and set @latestDate = balance[date_id] if the balance[date_id] was selected:

@latestDate ||= params[:balance[date_id]] unless params[:balance].blank?

Upvotes: 2

Related Questions