尽管在DB中创建了嵌套属性,但显示/编辑操作不会呈现嵌套表单的属性

时间:2016-01-18 22:54:51

标签: ruby-on-rails associations nested-attributes

我已经设置了一个嵌套表单,并且可以将表单保存到我的数据库中,但是当我尝试在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,如何在显示和编辑操作中将其连接到发票?

1 个答案:

答案 0 :(得分:1)

您需要嵌套资源:

NSExtension.NSExtensionMainStoryboard: Main

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

Main.storyboard

详细了解嵌套资源:Rails Routing from the Outside In: Nested Resources