是什么阻止了这个simple_form_for中的嵌套fields_for进行渲染?

时间:2021-04-18 19:33:15

I have a model Quote which has_many Employees and Employees belongs_to Quote. I am trying to build a nested simple_form_for form to update both of these models in my new_quote view, despite trying many syntax variations I cannot see what i'm doing wrong. The simple_fields_for content does not even render in the browser, everything else does? Any help on what I'm doing wrong would be greatly apprecited:

我有一个模型报价has_many员工和员工belongs_to报价。我正在尝试构建一个嵌套的simple_form_for表单来在我的new_quote视图中更新这两个模型,尽管我尝试了很多语法变化但我看不出我做错了什么。 simple_fields_for内容甚至没有在浏览器中呈现,其他一切都有效吗?对我所做错的任何帮助都会非常感激:

quotes_controller.rb

quotes_controller.rb

class QuotesController < ApplicationController
  before_action :authenticate_user!, only: [ :new, :create, :edit, :update, :destroy ]

  def new
    @quote = Quote.new
    @quote.employees.build

    respond_to do |format|
      format.html
      format.xlsx { response.headers['Content-Disposition'] = 'attachment; filename="empee_data.xlsx"' }
    end

  def quote_params
    params.require(:quote).permit(:gla, :prev_cover, :co_name, :co_number, :postcode, :industry, :lives_overseas, 
                                  :scheme_start_date, :payment_frequency, :commission_level)
  end
end

new.html.erb

new.html.erb

<div class='container'>
    <div class='row'>
        <h1>Complete the below to get a quote</h1>
        <%= render :partial => "new_quote"%>
    </div>
</div>

_new_quote.html.erb

_new_quote.html.erb

            <%= simple_form_for @quote do |f| %> 
        <div class='form-group col-md-6'>
            <%= f.input :gla, as: :boolean, label: "GLA" %>
            <%= f.input :prev_cover, as: :radio_buttons, collection: [['Yes', true], 
                        ['No', false]], readonly: nil, label: "Previous cover" %>
            <%= f.input :co_name, label: "Company name" %>
            <%= f.input :co_number, label: "Company number" %>
            <%= f.input :postcode %>
        </div>
        <div class='form-group col-md-6'>
            <%= f.input :lives_overseas, as: :radio_buttons, collection: [['Yes', true], 
                        ['No', false]], readonly: nil %>
            <%= f.input :industry, collection: Quote.industries.map { |k,v| [ k.humanize, k ] } %>
            <%= f.input :scheme_start_date %>
            <%= f.input :payment_frequency, collection: Quote.payment_frequencies.map { |k,v| [ k.humanize, k ] } %>
            <%= f.input :commission_level %>

            <% f.simple_fields_for :employees do |builder| %>
                <%= builder.input :first_name, label: "First name" %> 
                <%= builder.input :last_name, label: "Last name" %> 
                <%= builder.input :email, label: "Email" %>
                <%= builder.input :gender, collection: Employee.genders.map { |k,v| [ k.humanize, k ] } %> 
                <%= builder.input :date_of_birth %>
                <%= builder.input :salary %>
            <% end %>

            <%= link_to 'Download as .xlsx', new_quote_path(format: :xlsx) %>
        </div>
            <%= f.submit "Get quote", class: 'btn btn-primary' %>
        <% end %>

quote.rb

quote.rb

class Quote < ApplicationRecord
  belongs_to :user
  has_many :employees, inverse_of: :quote
  accepts_nested_attributes_for :employees
end

employees.rb

employees.rb

class Employee < ApplicationRecord
  belongs_to :quote
end

1 个解决方案

#1


1  

I mean that the fields_for element of the form doesn't show up, the rest of the form does, as if the fields_ for block didn't exist at all!? Any ideas?

我的意思是表单的fields_for元素没有显示,表单的其余部分也没有显示,就好像块的fields_根本不存在一样!?有任何想法吗?

This is because of this line

这是因为这条线

<% f.simple_fields_for :employees do |builder| %>

which should be

应该是

<%= f.simple_fields_for :employees do |builder| %>

You are missing =

你缺少=

#1


1  

I mean that the fields_for element of the form doesn't show up, the rest of the form does, as if the fields_ for block didn't exist at all!? Any ideas?

我的意思是表单的fields_for元素没有显示,表单的其余部分也没有显示,就好像块的fields_根本不存在一样!?有任何想法吗?

This is because of this line

这是因为这条线

<% f.simple_fields_for :employees do |builder| %>

which should be

应该是

<%= f.simple_fields_for :employees do |builder| %>

You are missing =

你缺少=