选择下拉列表中的参数值返回nil

时间:2015-08-09 11:06:35

标签: html ruby-on-rails forms post

我有一个表格。我表单中的其他所有内容都正确提交。

但是当我尝试获得params [:invest_with]的值时,我得到了什么?为什么是这样。这是我在表单中唯一的选择,所以也许我没有正确设置或者其他东西。

    <div class="form-group">
        <label class="sr-only">Invest_With</label>
        <div class="input-group">
          <div class="input-group-addon">Invest with: </div>

          <select name="invest_with" class="form-control">
            <option name="invest_with" value="user">Your Personal Balance</option>
            <% n = 0 %>
            <% @lender_companies.each do |company| %>
              <% n += 1 %>
              <option name="invest_with" value="company <%= n %>"> <%= company.name %>'s Balance </option>
            <% end %>
          </select>

        </div>
      </div>

以下是我的整个表格:

<form class="form-inline" method="post" action="/private_investement" accept-charset="utf-8">
  <div class="form-group">
    <label class="sr-only">Amount</label>
    <div class="input-group">
      <div class="input-group-addon">$</div>
      <input type="number" min="10" max="<%=@loan.pending%>" class="form-control" name="amount" placeholder="Amount" required>
    </div>
  </div>

<br><br>

  <div class="form-group">
    <div class="input-group">
      <div class="input-group-addon">Invest with: </div>

      <select name="invest_with" class="form-control" multiple required>
        <option value="user">Your Personal Balance</option>
        <%n=0%>
        <%@lender_companies.each do |company|%>
          <%n+=1%>
          <option value="company<%=n%>"><%=company.name%>'s Balance</option>
        <%end%>
      </select>

    </div>
  </div>


  <input type="hidden" name="loan_id" value="<%=@loan.id%>">
  <input type="hidden" name="company" value="<%=@company.name%>">
  <input type="hidden" name="loan_amount" value="<%=@loan.amount%>">
  <input type="hidden" name="investor_money" value="<%=current_user.balance%>">
  <input type="hidden" name="company_money" value="<%=@lender_company%>">

<br><br>
  <button type="sumbit" class="btn btn-primary">Invest</button><br>
  Once you press the invest button there is no coming back.
</form>

我的问题是如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

我相信你想要这样的东西。如果没有看到表单的其余部分,很难说清楚,但请确保正确设置了name属性;检查表单生成的其他元素,因为您可能需要将名称设置为user[invset_with]

<%= select_tag 'invest_with',
    options_for_select(@lender_companies.map{ |c| [c.name, "company #{c.id}"] }),
    {include_blank: 'Your Personal Balance', multiple: true, required: true, class: 'form-control'}
%>

另请确保permitting :invest_with参数

修改

在发布表格的简化版本后,终端会给出以下内容:

Started POST "/private_investement" for ::1 at 2015-08-09 17:41:51 -0400 Processing by StaticPagesController#private_investement as HTML Parameters: {"amount"=>"13", "invest_with"=>["company1", "company3"], "loan_id"=>"77", "company"=>"company.name", "loan_amount"=>"loan.amount", "investor_money"=>"balance", "company_money"=>"lender_company"}

如果您愿意提供您的日志包含的内容。