我的表单中有多个复选框,工作非常好。我需要提供更多选项来添加像四个chkbox,然后一个与它们相关的文本字段
我的型号看起来像
PaymentGateway.rb
has_many:payment_rfps
has_many:rfps,:through=>:payment_rfps
class PaymentRfp<ActiveRecord::基本
attr_accessible:payment_gateway_id,:rfp_id
belongs_to:payment_gateway
belongs_to:rfp结束
rfp.rb
has_many:payment_rfps
has_many:payment_gateways,:through=>:payment_rfps
我的视图部分
<div class = "lft_cms" ><b>Payment Gateways</b>
<div class="field">
<%= hidden_field_tag "rfp[ payment_gateway_ids][]", nil %>
<% PaymentGateway.all.each do |payment_gateway| %>
<%= check_box_tag "rfp[payment_gateway_ids][]",payment_gateway.id, @rfp.payment_gateway_ids.include?(payment_gateway.id), :id => dom_id(payment_gateway) %>
<%= label_tag dom_id(payment_gateway), payment_gateway.name %><br>
<% end %>
</div>
我如何添加一个文本字段以提供额外的填充选项感谢任何帮助。感谢
首先,PaymentGateway.all是个坏主意。这不应该出现在视图中,而是从实例变量中的控制器加载的。请更改。
一种方法是定义一个属性,比如说"additional_info":
<% PaymentGateway.all.each do |pg| %>
<%= check_box_tag ..., onclick: "$('#{pg}_info').show(); $('#{pg}_info').siblings().hide();" %>
...
<% end %>
<div class="additional_infos">
<%= f.input :gateway1_info, label: "Gateway" ... %>
</div>