【问题标题】:Pass selected value to Rails controller将选定的值传递给 Rails 控制器
【发布时间】: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


    【解决方案1】:

    “下拉菜单”是一种输入 (HTML &lt;select&gt;)。 &lt;input&gt;s 和 &lt;select&gt;s 必须在 &lt;form&gt; HTML 元素内。

    使用纯 HTML

    &lt;form&gt; 中的任何内容都可以在 params 哈希中访问。因此,如果您有一个下拉列表,它应该在 &lt;form&gt; 中,如下所示:

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

    在上面的示例中,所选值(例如“volvo”)将在 params["car"] 内的控制器中可用。请注意,您仍然需要定义 /yourroute

    使用 Rails 的辅助方法

    当然,Rails 可以帮助您生成表单和输入。为此,您可以使用 Rails 的form helpers。例如,下面会生成一个搜索表单(“搜索”标签、用于输入的文本框和一个按钮),它将 GET 发送到/search 路由:

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

    编辑

    如果选择了balance[date_id],则检查并设置@latestDate = balance[date_id]:

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

    【讨论】:

    • 感谢您的解释。我正在尝试使用辅助方法并单击“搜索”按钮,查询字符串会这样填充... ?utf8=✓&balance[date_id]=13%2F10%2F2015&commit=Search。是否可以只传递 balance[date_id] 值?
    • Rails 会自动生成一些 URL 参数,例如 utf8=✓。我不明白您为什么需要从 URL 中删除它们。有什么具体原因吗?
    • 只是为了整理一下。在检查参数是否存在,如果存在,读取它,... if (params[:balance[date_id]] != null) '@latestDate = params[:balance[date_id]] ... 返回 NameError在 BalancesController#statement 中。
    • 请更新您的代码,以便我提供进一步的建议。
    • 已经完成了。您会看到我已将下拉控件包装在表单助手中。
    猜你喜欢
    • 2021-11-12
    • 1970-01-01
    • 1970-01-01
    • 2022-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-06
    • 1970-01-01
    相关资源
    最近更新 更多