强大的参数无法从视图到导轨中的控制器

时间:2019-03-28 12:27:39

标签: ruby-on-rails ruby

我正在从视图发送参数,并在控制器中获取强大的参数。

我正在使用ruby版本的ruby 2.3.1尝试

在视图中-

<%= form_tag cardamoms_create_auction_path,:enctype =>"multipart/form-data" do %> 
    <div class="field">
        <table>
            <tr>
                <td>
                    <%= label_tag :company %>
                </td>
                <td>
                    <%= select_tag :company_id,options_for_select(@companies.collect{|c|[c.name,c.id]}),prompt:"Select company",class:"form-control",required: true%>
                </td>
            </tr>
            <tr>
                <td>
                    <%= label_tag "" %>
                </td>
            </tr>
            <tr>
                <td>
                    <%= label_tag :auction_number %>
                </td>
                <td>
                    <%=text_field_tag :auction_number,"",class: 'form-control',required: true,placeholder: "Ex. 19" %>
                </td>
            </tr>
            <tr>
                <td>
                    <%= label_tag "" %>
                </td>
            </tr>
            <tr>
                <td>
                    <%= label_tag "auction_date" %>
                </td>
                <td>
                    <%=text_field_tag :auction_date,"", "data-provide" => 'datepicker', class: "datepicker form-control",id: 'selectDate1',placeholder: "Auction date(yyyy-mm-dd)" ,required: true%>
                </td>
            </tr>
            <tr>
                <td>
                    <%= label_tag "image_upload" %>
                </td>
                <td>
                </td>
            </tr>
            <tr>
                <td>
                    <%= label_tag "start_date" %>
                </td>
                <td>
                    <%=text_field_tag :start_date,"", "data-provide" => 'datepicker', class: "datepicker form-control",id: 'selectDate2',placeholder: "Start date(yyyy-mm-dd)"%>
                </td>
            </tr>
            <tr>
                <td>
                    <%= label_tag "" %>
                </td>
            </tr>
            <tr>
                <td>
                    <%= label_tag "end_date" %>
                </td>
                <td>
                    <%=text_field_tag :end_date,"", "data-provide" => 'datepicker', class: "datepicker form-control",id: 'selectDate3',placeholder: "End date(yyyy-mm-dd)"%>
                </td>
            </tr>
        </table>
    </div>
    <div class="actions">
        <%= submit_tag "Submit",class:"btn btn-success" ,remote: true%>
    </div>
<% end %>

在控制器中-

private

def auction_params
  params.permit(:company_id,:auction_number,:auction_date,:start_date,:end_date)
end

我希望在控制器中-

private

def auction_params
  params.require(:auction).permit(:company_id,:auction_number,:auction_date,:start_date,:end_date)
end

如何以这样一种方式更改视图,使传入参数的格式为params[:auction][:company_id]而不是params[:company_id]

2 个答案:

答案 0 :(得分:1)

  

如何以这样的方式更改视图,即传入的参数   格式为params [:auction] [:company_id],而不是   params [:company_id]?

根据3limin4t0r的建议,您可以使用form_forform_with

或者您可以明确地将其设置为:

<%= select_tag :company_id,...

成为

<%= select_tag "auction[company_id]",...

其他标签也一样

答案 1 :(得分:1)

您应该使用form_for @object来实现。就像Wiki文档所说的那样,form_for创建了一个表单,该表单允许用户创建或更新特定模型对象的属性。 像这样:

<%= form_for(@post, url: super_posts_path) do |f| %>
  ...
<% end %>

应该可以解决问题。