是什么阻止了此 simple_form_for 中嵌套的 fields_for 的渲染?
What's stopping the nested fields_for in this simple_form_for from rendering?
我有一个模型报价,其中 has_many 员工和员工 belongs_to 报价。我正在尝试构建一个嵌套的 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
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?
这是因为这条线
<% f.simple_fields_for :employees do |builder| %>
应该是
<%= f.simple_fields_for :employees do |builder| %>
你不见了=
我有一个模型报价,其中 has_many 员工和员工 belongs_to 报价。我正在尝试构建一个嵌套的 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
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?
这是因为这条线
<% f.simple_fields_for :employees do |builder| %>
应该是
<%= f.simple_fields_for :employees do |builder| %>
你不见了=