Rails访问属于模型的选定项

时间:2016-04-30 22:59:43

标签: ruby-on-rails ruby forms

在我的应用程序中,我有以下模型:

# deposit.rb
class Deposit < ActiveRecord::Base
end

# account.rb
class Accounts < ActiveRecord::Base
  has_may :payments
end

# payment.rb
class Payment < ActiveRecord::Base
  belongs_to :account
end

在新的存款模板中,我正在循环使用属于特定帐户的付款:

# deposits_controller.rb
class DepositsController < ApplicationController
  def new
    @account = Account.find_by(name: "Foo")
    @payments = @account.payments
  end

  def create
    # i need to access the selected records here
  end
end

# new.html.erb
<%= form_tag do |f| %>
  <table class="table">
    <thead class="thead-default">
      <th>
      </th>
      <th>Received From</th>
      <th>Date</th>
      <th>Amount</th>
    </thead>

    <tbody>
      <% @payments.each do |p| %>
        <tr>
          <td>
            <%= check_box_tag :selected %>
          </td> 
          <td><%= number_to_currency(p.amount) %></td>
        </tr>
      <% end %>
    </tbody>
  </table>

  <%= submit_tag %>
<% end %>

我需要一种方法来访问每个付款,其中在提交表单时选中selected复选框。实现这一目标的最佳方法是什么?

1 个答案:

答案 0 :(得分:1)

使用Ctrl构建表单,之后,您可以通过form_for变量访问值

params