Rails嵌套属性已创建,但字段保持为空

时间:2013-10-21 13:35:09

标签: ruby-on-rails postgresql ruby-on-rails-4 nested-attributes

这是我的两个模型,没什么复杂的。

class Invoice
  belongs_to :user
  has_many :invoice_line_items
  accepts_nested_attributes_for :invoice_line_items, allow_destroy: true
end

class InvoiceLineItem
  belongs_to :invoice
end

这是嵌套属性的修改后的simple_form。

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

  <div class="form-inputs">
    <%= f.input :adress_sender %>
    <%= f.input :adress_recipient %>
    <%= f.input :status %>
    <%= simple_fields_for :invoice_line_items do |invoice_line_items_form| %>
      <%= invoice_line_items_form.input :description %>
      <%= invoice_line_items_form.input :price %>
      <%= invoice_line_items_form.input :amount %>
    <% end %>
  </div>  
  <div class="form-actions">
    <%= f.button :submit %>
  </div>
<% end %>

根据rails的官方文档,这是控制器。

class InvoicesController < ApplicationController
  before_action :set_invoice, only: [:show, :edit, :update, :destroy]

  # GET /invoices
  # GET /invoices.json
  def index
    @invoices = current_user.invoices
  end

  # GET /invoices/1
  # GET /invoices/1.json
  def show
  end

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

  # GET /invoices/1/edit
  def edit
  end

  # POST /invoices
  # POST /invoices.json
  def create
    @invoice = current_user.invoices.new(invoice_params)
    @invoice.invoice_line_items.build
    respond_to do |format|
      if @invoice.save
        format.html { redirect_to @invoice, notice: 'Invoice was successfully created.' }
        format.json { render action: 'show', status: :created, location: @invoice }
      else
        format.html { render action: '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 { head :no_content }
      else
        format.html { render action: '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 }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions and check if the user has access to it.
    def set_invoice
        @invoice = current_user.invoices.find(params[:id])
        @invoice.invoice_line_items.build
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def invoice_params
      params.require(:invoice).permit(:adress_sender, :adress_recipient, :status, :user_id, :customer_id, invoice_line_items: [:description, :price, :amount])
    end
end

正在创建发票行项目。但只是创建和更新日期和ID。描述,价格和金额等所有字段均为空。另外allow_destroy似乎不起作用。不幸的是,服务器控制台中没有错误:/

编辑,如建议的控制台日志所示。

Started POST "/invoices" for 127.0.0.1 at 2013-10-21 15:36:42 +0200
Processing by InvoicesController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"mFdXPbYF+Lsb1mugWkdykBkJ1iSrzZoREL5Alw6phhQ=", "invoice"=>{"adress_sender"=>"awfawf", "adress_recipient"=>"awfgaw", "status"=>"awgag"}, "invoice_line_items"=>{"description"=>"awga", "price"=>"awgwa", "amount"=>"awg"}, "commit"=>"Create Invoice"}
  User Load (2.1ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 1 ORDER BY "users"."id" ASC LIMIT 1
   (0.3ms)  BEGIN
  SQL (7.0ms)  INSERT INTO "invoices" ("adress_recipient", "adress_sender", "created_at", "status", "updated_at", "user_id") VALUES ($1, $2, $3, $4, $5, $6) RETURNING "id"  [["adress_recipient", "awfgaw"], ["adress_sender", "awfawf"], ["created_at", Mon, 21 Oct 2013 13:36:42 UTC +00:00], ["status", "awgag"], ["updated_at", Mon, 21 Oct 2013 13:36:42 UTC +00:00], ["user_id", 1]]
  SQL (0.9ms)  INSERT INTO "invoice_line_items" ("created_at", "invoice_id", "updated_at") VALUES ($1, $2, $3) RETURNING "id"  [["created_at", Mon, 21 Oct 2013 13:36:42 UTC +00:00], ["invoice_id", 15], ["updated_at", Mon, 21 Oct 2013 13:36:42 UTC +00:00]]
   (4.2ms)  COMMIT
Redirected to http://localhost:3000/invoices/15
Completed 302 Found in 60ms (ActiveRecord: 14.5ms)

有任何想法或建议吗?

最好的问候 denym

3 个答案:

答案 0 :(得分:1)

首先与您的问题略有不同 - 对于set_invoice调用,您始终会创建新的invoice_line_item,即使在销毁时也是如此。如果您尝试包含给定发票的所有invoice_line_items,您应该执行类似@invoice.includes(:invoice_line_items).where(id: params[:id])这样的操作,这应该会急切加载这些项目。

关于你的问题...对于incoive_params,invoice_line_items应该是invoice_line_items_attributes(包括:更新的ID也很好。)

试一下,看看它是否有效。同样在创建操作中,我认为您不需要添加@invoice.invoice_line_items.build,因为您已经在上面的命令中包含了嵌套表单的参数。通过执行@invoice.invoice_line_items.build看起来你只是为@invoice创建了另一个invoice_line_item但没有对params进行语音化。

(稍微写了一下回复。希望这是有道理的。)

def create
  @invoice = current_user.invoices.new(invoice_params)
  @invoice.invoice_line_items.build
  respond_to do |format|
    if @invoice.save
      format.html { redirect_to @invoice, notice: 'Invoice was successfully created.' }
      format.json { render action: 'show', status: :created, location: @invoice }
    else
      format.html { render action: 'new' }
      format.json { render json: @invoice.errors, status: :unprocessable_entity }
    end
  end
end

# Never trust parameters from the scary internet, only allow the white list through.
def invoice_params
  params.require(:invoice).permit(:adress_sender, :adress_recipient, :status, :user_id, :customer_id, invoice_line_items_attributes: [:id, :description, :price, :amount])
end

```

旁注:地址拼写错误。不知道这是否会导致铁路多元化的麻烦。

答案 1 :(得分:0)

尝试以下内容我希望它能为您提供帮助。

def new
    @invoice = Invoice.new
    @invoice.invoice_line_items.build
  end

  def create
    @invoice = current_user.invoices.create(invoice_params)

    respond_to do |format|
      if @invoice.save
        format.html { redirect_to @invoice, notice: 'Invoice was successfully created.' }
        format.json { render action: 'show', status: :created, location: @invoice }
      else
        format.html { render action: 'new' }
        format.json { render json: @invoice.errors, status: :unprocessable_entity }
      end
    end
  end

答案 2 :(得分:0)

除了已经给出的答案之外,您应该尝试f.simple_fields_for而不是simple_fields_for来表示嵌套结构。否则,使用@invoice.invoice_line_items.build构建的项目不会填写表单数据:

<%= f.simple_fields_for :invoice_line_items do |invoice_line_items_form| %>
  <%= invoice_line_items_form.input :description %>
  <%= invoice_line_items_form.input :price %>
  <%= invoice_line_items_form.input :amount %>
<% end %>

与改变的创造行动一起(见Amit Sharma的帖子):

def create
  @invoice = current_user.invoices.create(invoice_params)
  respond_to do |format|
  if @invoice.save
    format.html { redirect_to @invoice, notice: 'Invoice was successfully created.' }
    format.json { render action: 'show', status: :created, location: @invoice }
  else
    format.html { render action: 'new' }
    format.json { render json: @invoice.errors, status: :unprocessable_entity }
  end
end

和“invoice_params”的更改(见8bithero的帖子):

def invoice_params
 params.require(:invoice).permit(:adress_sender, :adress_recipient, :status, :user_id, :customer_id, invoice_line_items_attributes: [:id, :description, :price, :amount])
end

这应该可以解决问题。