0
  <%= simple_form_for @order, :html => { :class => 'form-horizontal' } do |f| %>
  <%= f.input :contact %>
  <%= f.simple_fields_for :products do |product| %>
  <%= render 'product_fields', :f => product %>
  <% end %>
  <%= link_to_add_association 'add product', f, :products %>
 <%= f.button :submit, :class => 'btn-primary' %>
 <%= link_to t('.cancel', :default => t("helpers.links.cancel")),
            orders_path, :class => 'btn' %>
 <% end %>

product_field_for

<%= f.input :name %>

def create
@order = Order.new(order_params)

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

def order_params
  params.require(:order).permit(:contact, { :products => [:id, :name, :description, :success, :count, :spot, :janpan_price, :taiwan_price, :_destroy ]}).merge(:seller_id => current_seller.id)
end

在模型中

class Order < ActiveRecord::Base
belongs_to :seller
has_many :products
end

class Product < ActiveRecord::Base
belongs_to :order 
end

但是当我提交创建

ActiveRecord::AssociationTypeMismatch in OrdersController#create Product(#70251926707520) 预期,得到 Array(#70251924853020)

1 # POST /orders.json

2 def create

3 @order = Order.new(order_params)

4 respond_to do |format|

5 if @order.save

它将在第 3 行中断

服务器日志

    Started POST "/orders" for 127.0.0.1 at 2013-09-13 18:49:37 +0800
Processing by OrdersController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"gHR42nO/ivCaOOGdpEvVEo07UNwmUmBw5uoHs+dN+fc=", "order"=>{"export"=>"0", "contact"=>"", "exchange_rate"=>"", "fare"=>"", "fee"=>"", "totalprice"=>"", "bank_name"=>"", "bank_account"=>""}, "commit"=>"Create Order"}
  Seller Load (0.2ms)  SELECT "sellers".* FROM "sellers" WHERE "sellers"."id" = 3 ORDER BY "sellers"."id" ASC LIMIT 1
   (0.1ms)  begin transaction
  SQL (2.6ms)  INSERT INTO "orders" ("bank_account", "bank_name", "contact", "created_at", "export", "seller_id", "updated_at") VALUES (?, ?, ?, ?, ?, ?, ?)  [["bank_account", ""], ["bank_name", ""], ["contact", ""], ["created_at", Fri, 13 Sep 2013 10:49:37 UTC +00:00], ["export", false], ["seller_id", 3], ["updated_at", Fri, 13 Sep 2013 10:49:37 UTC +00:00]]
   (1.8ms)  commit transaction
Redirected to http:// /orders/34
Completed 302 Found in 11ms (ActiveRecord: 4.6ms)


Started GET "/orders/34" for 127.0.0.1 at 2013-09-13 18:49:37 +0800
Processing by OrdersController#show as HTML
  Parameters: {"id"=>"34"}
  Order Load (0.3ms)  SELECT "orders".* FROM "orders" WHERE "orders"."id" = ? LIMIT 1  [["id", "34"]]
  Seller Load (0.2ms)  SELECT "sellers".* FROM "sellers" WHERE "sellers"."id" = 3 ORDER BY "sellers"."id" ASC LIMIT 1
  Rendered orders/show.html.erb within layouts/application (6.7ms)
Completed 500 Internal Server Error in 13ms

ActionView::Template::Error (undefined method `product' for #<Order:0x007f97e6365260>):
    15:   <dt><strong><%= model_class.human_attribute_name("手續費") %>:</strong></dt>
    16:   <dd><%= @order.fee %></dd>
    17:   <dt><strong><%= model_class.human_attribute_name("商品網頁") %>:</strong></dt>
    18:   <dd><%= @order.product.name %></dd>
    19:   <dt><strong><%= model_class.human_attribute_name("備註") %>:</strong></dt>
    20:   <dd><%= @order.description %></dd>
    21:   <dt><strong><%= model_class.human_attribute_name("數量") %>:</strong></dt>
  app/views/orders/show.html.erb:18:in `_app_views_orders_show_html_erb__256659907252631377_70145189586620'


  Rendered /Users/mac/.rvm/gems/ruby-2.0.0-p247/gems/actionpack-4.0.0/lib/action_dispatch/middleware/templates/rescues/_trace.erb (1.2ms)
  Rendered /Users/mac/.rvm/gems/ruby-2.0.0-p247/gems/actionpack-4.0.0/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (0.9ms)
  Rendered /Users/mac/.rvm/gems/ruby-2.0.0-p247/gems/actionpack-4.0.0/lib/action_dispatch/middleware/templates/rescues/template_error.erb within rescues/layout (11.2ms)
4

1 回答 1

0

在订单模型:

accepts_nested_attributes_for :products, allow_destroy: true

并允许products_attributes: [...]order_params方法

我认为,您的order_params方法应如下所示:

def order_params
  params.require(:order).permit(:contact, products_attributes: [:id, :name, :description, :success, :count, :spot, :janpan_price, :taiwan_price, :_destroy ]).merge(:seller_id => current_seller.id)
end
于 2013-09-13T09:17:47.267 回答