0

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

1 Answer 1

2

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?
Sign up to request clarification or add additional context in comments.

9 Comments

Thanks for the explanation. I'm attempting to use the helper method and clicking on the Search button, the querystring gets populated as such ... ?utf8=✓&balance[date_id]=13%2F10%2F2015&commit=Search. Is it possible to only pass the balance[date_id] value?
Rails generates some URL arguments such as utf8=✓ automatically. I don't understand why would you need to remove them from the URL. Is there any specific reason?
Just to tidy it up. In terms of checking if the argument exists and if so, reading it, ... if (params[:balance[date_id]] != null) '@latestDate = params[:balance[date_id]] ... returns a NameError in BalancesController#statement.
Please update your code so that I can give further suggestions.
Have done. You'll see I've wrapped the dropdown control in a form helper.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.