无法将值传递给rails form_tag形式的参数

时间:2016-08-11 00:40:06

标签: ruby-on-rails ruby-on-rails-4

我有一个form_tag表单(new.html.erb),如下所示:

<% provide(:title, "Initiate a Transaction") %>
<h1>New Transaction</h1>
<div class="row">
  <div class="col-md-6 col-md-offset-3">
    <%= form_tag '/fund_orders' do %>
      <div class="field">
        <%= label_tag :investment_type, "Transaction Type" %>
        <%= select_tag :investment_type, options_from_collection_for_select(@investment_type, 'id', 'transaction_type'), prompt: "--Select--" %>
      </div>
      <% @orders.each_with_index do |fund_order, index| %>
        <% fieldname = 'fund_orders' + '[' + index.to_s + ']' %>
        <%= fields_for fieldname, fund_order do |f| %>
          <%= render 'fund_order_details', fund_order: f %>
        <% end %>
      <% end %>
      <div class="actions">
        <%= submit_tag "Initiate Transaction", class: "btn btn-primary" %>
      </div>
    <% end %>
  </div>
</div>

我正在尝试通过此表单创建多个新事务。 @orders.each_with_index中的一段代码 block是处理事务的表单字段的部分。但是 - 你会注意到我在顶部有一个select_tag。提交表单后 - params会填充每个@order的数据(正如我在开发控制台中看到的那样),但它不包含params[:investment_type]。这里出了什么问题?

我尝试完全删除@orders.each_with_index块,即使这样params[:investment_type]也没有通过。想法?

更新 我在顶部更新了视图。另外 - 这是从视图中调用的fund_order_details部分:

<fieldset>
  <div class="field">
    <%= fund_order.label :fund_house, "Fund House" %>
    <%= fund_order.collection_select(:fund_house, FundHouse.where(real_fund_house: true), :id, :fund_house,
                            {:prompt => "--Select--", :class => "fund_house"},
                            {:data => {:remote => true,
                                        :url => url_for(controller: "fund_orders",
                                                        action: "update_fundnames")
                                        }})%>
  </div>
  <div class="field">
    <%= fund_order.label :fund, "Fund Name" %>
    <%= fund_order.collection_select(:fund, MutualFund.none, :id, :fund_name,
                              {:prompt => "--Select--", :class => "fund_name"})%>
  </div>
  <div class="field">
    <%= fund_order.label :amount %>
    <%= fund_order.number_field(:amount, in: 1000..99999, step: 500) %>
  </div>
  <div class="field">
    <%= fund_order.label :start_date, "Starting date" %>
    <%= fund_order.text_field :start_date, placeholder: "DD/MM/YYYY", required: true  %>
  </div>
</fieldset>

@ taryn-east - 这就是HTML的样子。

<div class="container">
      <h1>New Transaction</h1>
<div class="row">
  <div class="col-md-6 col-md-offset-3">
    <form action="/fund_orders/" accept-charset="UTF-8" method="post"><input name="utf8" type="hidden" value="✓"><input type="hidden" name="authenticity_token" value="HcHWLUTlLpMLMhgtdcFVumLvvD1DqmTdLvmr+Imm3Thlc+nZr0BqYjm30Gq9b+stemaylbIN83JpbEnL0cy3dg==">
      <div class="field">
        <label for="investment_type">Transaction Type</label>
        <select name="investment_type" id="investment_type" disabled="disabled"><option value="">--Select--</option><option value="1">Purchase</option>
<option value="2">Redeem</option>
<option value="3">SIP</option>
<option value="4">SWP</option>
<option value="5">STP</option>
<option value="6">Switch</option></select>
      </div>
        
          <fieldset style="display: block;">
  <div class="field">
    <label for="fund_orders_0_fund_house">Fund House</label>
    <select data-remote="true" data-url="/update_fundnames" name="fund_orders[0][fund_house]" id="fund_orders_0_fund_house"><option value="">--Select--</option>
<option value="1">ICICI</option>
<option value="2">SBI</option>
<option value="3">Axis</option>
<option value="4">HDFC</option>
<option value="5">Reliance</option></select>
  </div>
  <div class="field">
    <label for="fund_orders_0_fund">Fund Name</label>
    <select name="fund_orders[0][fund]" id="fund_orders_0_fund"><option value="">--Select--</option>
</select>
  </div>
  <div class="field">
    <label for="fund_orders_0_amount">Amount</label>
    <input step="500" min="1000" max="99999" type="number" name="fund_orders[0][amount]" id="fund_orders_0_amount">
  </div>
  <div class="field">
    <label for="fund_orders_0_start_date">Starting date</label>
    <input placeholder="DD/MM/YYYY" required="required" type="text" name="fund_orders[0][start_date]" id="fund_orders_0_start_date">
  </div>
</fieldset>
        
          <fieldset style="display: none;">
  <div class="field">
    <label for="fund_orders_1_fund_house">Fund House</label>
    <select data-remote="true" data-url="/update_fundnames" name="fund_orders[1][fund_house]" id="fund_orders_1_fund_house"><option value="">--Select--</option>
<option value="1">ICICI</option>
<option value="2">SBI</option>
<option value="3">Axis</option>
<option value="4">HDFC</option>
<option value="5">Reliance</option></select>
  </div>
  <div class="field">
    <label for="fund_orders_1_fund">Fund Name</label>
    <select name="fund_orders[1][fund]" id="fund_orders_1_fund"><option value="">--Select--</option>
</select>
  </div>
  <div class="field">
    <label for="fund_orders_1_amount">Amount</label>
    <input step="500" min="1000" max="99999" type="number" name="fund_orders[1][amount]" id="fund_orders_1_amount">
  </div>
  <div class="field">
    <label for="fund_orders_1_start_date">Starting date</label>
    <input placeholder="DD/MM/YYYY" required="required" type="text" name="fund_orders[1][start_date]" id="fund_orders_1_start_date">
  </div>
</fieldset>
      <div class="actions">
        <input type="submit" name="commit" value="Initiate Transaction" class="btn btn-primary">
      </div>
</form>  </div>
</div>

<footer></footer>

我简化了视图 - 但现在添加了完整的HTML。

此外 - 以下是传递给服务器日志的参数:

 Parameters: {"utf8"=>"✓", "authenticity_token"=>"HcHWLUTlLpMLMhgtdcFVumLvvD1DqmTdLvmr+Imm3Thlc+nZr0BqYjm30Gq9b+stemaylbIN83JpbEnL0cy3dg==", "fund_orders"=>{"0"=>{"fund_house"=>"", "fund"=>"", "amount"=>"", "start_date"=>"10/08/2016"}, "1"=>{"fund_house"=>"", "fund"=>"", "amount"=>"", "start_date"=>"10/08/2016"}}, "commit"=>"Initiate Transaction"}

现在 - 我只是试图正确设置视图并将我需要的所有参数从视图传递给控制器​​。我还没有任何强大的params需要在控制器中进行设置。请查看这是否足够详细信息。我还编辑了上面的.html.erb视图代码,以显示它的整体外观。

1 个答案:

答案 0 :(得分:0)

啊哈,html有线索:

<select name="investment_type" id="investment_type" disabled="disabled">

您的浏览器无法通过已禁用字段中的任何值发送...所以......现在您需要弄清楚该字段被禁用的原因。