嵌套形式(茧 gem)在视图中不可见 rails
Nested form(cocoon gem) not visible in view rails
我用 cocoon gem 为我的发票申请制作了一个嵌套表格,但该表格没有显示在我的申请中,但它也没有给出任何错误。
_form.html.erb - 支架形式部分
<address>
<%= f.fields_for :customer do |customer| %>
<%= render 'customer_fields', f: customer %>
<%= link_to_add_association 'Add customer',f, :customer %>
<% end %>
</address>
_customer_fields.html.erb - 茧部分
<div class="nested-fields">
<div class="form-group">
<%= f.label 'Company Name' %><br/>
<%= f.text_field :company_name, placeholder: 'company name' %>
</div>
<div class="form-group">
<%= f.label 'Address' %><br>
<%= f.text_field :address_line_1, placeholder: 'address' %>
</div>
<div class="form-group">
<%= f.label 'Zip Code' %><br>
<%= f.text_field :zip_code %>
</div>
<%= link_to_remove_association "remove customer", f, class: 'btn btn-primary' %>
</div>
Invoice.rb 型号
class Invoice < ActiveRecord::Base
has_one :company
has_one :customer
has_many :products
accepts_nested_attributes_for :customer, reject_if: :all_blank, allow_destroy: true
validates :number, :currency, :date, :duedate, :btwtotal, :subtotal, :total, presence: true
end
customer.rb 型号
class Customer < ActiveRecord::Base
belongs_to :invoice
end
Invoices_controller.rb
class InvoicesController < ApplicationController
before_action :set_invoice, only: [:show, :edit, :update, :destroy]
# GET /invoices
# GET /invoices.json
def index
@invoices = Invoice.all
end
# GET /invoices/1
# GET /invoices/1.json
def show
end
# GET /invoices/new
def new
@invoice = Invoice.new
end
# GET /invoices/1/edit
def edit
end
# POST /invoices
# POST /invoices.json
def create
@invoice = Invoice.new(invoice_params)
respond_to do |format|
if @invoice.save
format.html { redirect_to @invoice, notice: 'Invoice was successfully created.' }
format.json { render :show, status: :created, location: @invoice }
else
format.html { render :new }
format.json { render json: @invoice.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /invoices/1
# PATCH/PUT /invoices/1.json
def update
respond_to do |format|
if @invoice.update(invoice_params)
format.html { redirect_to @invoice, notice: 'Invoice was successfully updated.' }
format.json { render :show, status: :ok, location: @invoice }
else
format.html { render :edit }
format.json { render json: @invoice.errors, status: :unprocessable_entity }
end
end
end
# DELETE /invoices/1
# DELETE /invoices/1.json
def destroy
@invoice.destroy
respond_to do |format|
format.html { redirect_to invoices_url, notice: 'Invoice was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_invoice
@invoice = Invoice.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def invoice_params
params.require(:invoice).permit(:number, :currency, :date, :duedate, :btwtotal,
:subtotal, :total, :footer, customers_attributes: [:id, :company_name, :address_line_1, :zip_code, :_destroy],
companies_attributes: [:id, :btw_number, :iban_number, :kvk_number, :company_name, :_destroy])
end
end
有人知道如何解决这个问题吗?任何帮助将不胜感激!
试一试
<address>
<%= f.object.build_customer if f.object.customer.nil? %>
<%= f.fields_for :customer do |customer| %>
<%= render 'customer_fields', f: customer %>
<%= link_to_add_association 'Add customer',f, :customer %>
<% end %>
</address>
编辑
在 Luissimo 解决方案中缺少在 customer
模式中插入 invoice_id
rails generate migration AddInvoiceIdToCustomer invoice_id:integer
rake db:migrate
Invoice.first.build_customer
查看您的原始代码,您应该在控制器中添加:
def new
@invoice = Invoice.new
@invoice.customer.build
end
我用 cocoon gem 为我的发票申请制作了一个嵌套表格,但该表格没有显示在我的申请中,但它也没有给出任何错误。
_form.html.erb - 支架形式部分
<address>
<%= f.fields_for :customer do |customer| %>
<%= render 'customer_fields', f: customer %>
<%= link_to_add_association 'Add customer',f, :customer %>
<% end %>
</address>
_customer_fields.html.erb - 茧部分
<div class="nested-fields">
<div class="form-group">
<%= f.label 'Company Name' %><br/>
<%= f.text_field :company_name, placeholder: 'company name' %>
</div>
<div class="form-group">
<%= f.label 'Address' %><br>
<%= f.text_field :address_line_1, placeholder: 'address' %>
</div>
<div class="form-group">
<%= f.label 'Zip Code' %><br>
<%= f.text_field :zip_code %>
</div>
<%= link_to_remove_association "remove customer", f, class: 'btn btn-primary' %>
</div>
Invoice.rb 型号
class Invoice < ActiveRecord::Base
has_one :company
has_one :customer
has_many :products
accepts_nested_attributes_for :customer, reject_if: :all_blank, allow_destroy: true
validates :number, :currency, :date, :duedate, :btwtotal, :subtotal, :total, presence: true
end
customer.rb 型号
class Customer < ActiveRecord::Base
belongs_to :invoice
end
Invoices_controller.rb
class InvoicesController < ApplicationController
before_action :set_invoice, only: [:show, :edit, :update, :destroy]
# GET /invoices
# GET /invoices.json
def index
@invoices = Invoice.all
end
# GET /invoices/1
# GET /invoices/1.json
def show
end
# GET /invoices/new
def new
@invoice = Invoice.new
end
# GET /invoices/1/edit
def edit
end
# POST /invoices
# POST /invoices.json
def create
@invoice = Invoice.new(invoice_params)
respond_to do |format|
if @invoice.save
format.html { redirect_to @invoice, notice: 'Invoice was successfully created.' }
format.json { render :show, status: :created, location: @invoice }
else
format.html { render :new }
format.json { render json: @invoice.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /invoices/1
# PATCH/PUT /invoices/1.json
def update
respond_to do |format|
if @invoice.update(invoice_params)
format.html { redirect_to @invoice, notice: 'Invoice was successfully updated.' }
format.json { render :show, status: :ok, location: @invoice }
else
format.html { render :edit }
format.json { render json: @invoice.errors, status: :unprocessable_entity }
end
end
end
# DELETE /invoices/1
# DELETE /invoices/1.json
def destroy
@invoice.destroy
respond_to do |format|
format.html { redirect_to invoices_url, notice: 'Invoice was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_invoice
@invoice = Invoice.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def invoice_params
params.require(:invoice).permit(:number, :currency, :date, :duedate, :btwtotal,
:subtotal, :total, :footer, customers_attributes: [:id, :company_name, :address_line_1, :zip_code, :_destroy],
companies_attributes: [:id, :btw_number, :iban_number, :kvk_number, :company_name, :_destroy])
end
end
有人知道如何解决这个问题吗?任何帮助将不胜感激!
试一试
<address>
<%= f.object.build_customer if f.object.customer.nil? %>
<%= f.fields_for :customer do |customer| %>
<%= render 'customer_fields', f: customer %>
<%= link_to_add_association 'Add customer',f, :customer %>
<% end %>
</address>
编辑
在 Luissimo 解决方案中缺少在 customer
模式中插入 invoice_id
rails generate migration AddInvoiceIdToCustomer invoice_id:integer
rake db:migrate
Invoice.first.build_customer
查看您的原始代码,您应该在控制器中添加:
def new
@invoice = Invoice.new
@invoice.customer.build
end