【发布时间】:2015-12-17 21:19:58
【问题描述】:
我在余额控制器 (balances_controller.rb) 中有一个名为“statement”的方法,它将特定日期 (@latestDate) 的信息返回到视图 (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 设置为从下拉列表中选择的值。
我该怎么做?
更新:
我根据 brito 的建议修改了我的代码。当我按下搜索按钮时,表单会传递以下参数:
?utf8=✓&date_id=30%2F12%2F2015&commit=Search
但是,如果我选择一个日期并单击“搜索”,@latestDate 会被设置并且 h1 标记会正确显示,但不会返回其余数据。
代码:
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>
....
【问题讨论】:
标签: ruby-on-rails ruby