Show/edit 尽管正在数据库中创建嵌套属性,但操作不呈现嵌套表单的属性

Show/edit action not rendering attributes of nested form although nested attributes are being created in DB

我已经设置了一个嵌套表单,并且能够将表单保存到我的数据库中,但是当我尝试在 show/edit 操作中呈现表单数据时,表单字段是空白的。

这是我的模型

invoice.rb
class Invoice < ActiveRecord::Base
has_many :items
accepts_nested_attributes_for :items
end

item.rb
class Item < ActiveRecord::Base
belongs_to :invoice
accepts_nested_attributes_for :invoice
end

还有我的控制器

def index
@invoices = Invoice.all
end

def show
@invoice = Invoice.find(params[:id])
@item = @invoice.items.build
end

# GET /invoices/new
def new
@invoice = Invoice.new
@invoice.items.build #
end

# GET /invoices/1/edit
def edit
@invoice.items.build #
end

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


 private
 def set_invoice
  @invoice = Invoice.find(params[:id])
 end

 def invoice_params
  params.require(:invoice).permit(:amount, :company, :contragent, :currency, :date, items_attributes: [ :item_name, :item_description, :item_cost, :item_quantity, :item_price ])
end
end

这是我的表格

  <%= simple_form_for(@invoice) do |f| %>

   <%= f.input_field :amount, class: "form-control" %>
   <%= f.input_field :company, class: "form-control" %>
   <%= f.input_field :currency, class: "form-control" %>
   <%= f.input_field :contragent, class: "form-control" %>
   <%= f.label :date, required: false %>

    <%= f.fields_for :items do |h| %>
    <%= h.error_notification %>
    <%= h.label :item_name %>
    <%= h.input_field :item_name, class: "form-control" %>
    <%= h.label :item_description %>
    <%= h.input_field :item_description, class: "form-control" %>
    <%= h.label :item_cost %>
    <%= h.input_field :item_cost, class: "form-control" %>
    <%= h.label :item_quantity %>
    <%= h.input_field :item_quantity, class: "form-control" %>
    <%= h.label :item_price %>
    <%= h.input_field :item_price, class: "form-control" %>
    </br>
   <% end %>

   <%= f.button :submit, 'Submit Payment', class: 'btn btn-warning btn-sm', id: "submit_invoice" %>    
<% end %>

保存表格后,我在 rails 控制台中得到了这个

2.1.1 :018 > p = Invoice.where(id: 38).limit(1)
Invoice Load (0.1ms)  SELECT  "invoices".* FROM "invoices" WHERE "invoices"."id" = ? LIMIT 1  [["id", 38]]
=> #<ActiveRecord::Relation [#<Invoice id: 38, amount: #<BigDecimal:7f8b6994b000,'0.875E3',9(18)>, company: "Alex", contragent: "Hi", currency: "NZD", date: "2016-02-17", created_at: "2016-01-18 22:23:36", updated_at: "2016-01-18 22:23:36">]>


2.1.1 :026 > p = Item.where(id: 7).limit(1)
Item Load (0.1ms)  SELECT  "items".* FROM "items" WHERE "items"."id" = ? LIMIT 1  [["id", 7]]
 => #<ActiveRecord::Relation [#<Item id: 7, item_name: "Test", item_description: "This is a test", item_cost: "200", item_quantity: "1", item_price: "400", created_at: "2016-01-18 22:23:36", updated_at: "2016-01-18 22:23:36", invoice_id: 38>]> 
2.1.1 :027 >

所以发票 ID 是在项目记录上设置的,我如何将它连接到显示和编辑操作中的发票?

您需要嵌套资源:

resources :invoices do
  resources :items
end

这将允许您的路由器访问发票中的项目:

/invoices/1/items/38

阅读有关嵌套资源的更多信息:Rails Routing from the Outside In: Nested Resources