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

时间:2017-06-20 21:06:19

标签: ruby-on-rails simple-form nested-forms

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

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

<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

            <%= 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

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

employees.rb

class Employee < ApplicationRecord
  belongs_to :quote
end

1 个答案:

答案 0 :(得分:1)

  

我的意思是表单的fields_for元素没有显示出来   表单的其余部分确实如此,就好像块中的fields_不存在一样   所有!?有什么想法吗?

这是因为这一行

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

应该是

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

您缺少=