【发布时间】:2016-08-11 00:40:06
【问题描述】:
我有一个 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 中的一段代码
块是处理交易表单字段的部分。但是 - 你会注意到我在顶部有一个 select_tag。提交表单后 - 参数填充了每个@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"}
现在 - 我只是尝试正确设置视图并将我需要的所有参数从视图传递给控制器。我还没有任何强大的参数需要控制器中的设置。请看看这是否足够详细。我还编辑了上面的视图 .html.erb 代码以显示它的整体外观。
【问题讨论】:
-
您好,欢迎来到 Stack Overflow!您能否编辑您的问题并添加一些可能有助于我们找出问题所在的内容?最好的办法是让您查看您的服务器日志并将我们作为正在通过的参数的示例。之后有时还会出现错误(例如“Unpermitted params”) - 您是否检查过该字段是否在您的控制器中的
permit/require中?如果您可以向我们展示为您的表单生成的 html,它也可能很有用......我们可以从它开始,看看它是如何进行的 :) -
@TarynEast - 我已经用更多细节更新了这个问题。
标签: ruby-on-rails ruby-on-rails-4