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