0

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

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)

有什么想法或建议吗?

最好的问候否认

4

3 回答 3

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,但没有对参数进行语音化。

(写的有点匆忙。希望它有意义。)

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

```

旁注:地址拼写错误。不知道这样会不会给铁路多元化带来麻烦。

于 2013-10-21T14:01:44.040 回答
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
于 2013-10-21T13:54:52.327 回答
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

这应该可以解决问题。

于 2013-10-21T14:13:22.977 回答