未经许可的参数 - Rails 4.1.1,Ruby 2.1.2

时间:2014-06-06 23:33:19

标签: ruby ruby-on-rails-4 nested-attributes strong-parameters

我似乎无法找到我出错的地方。我的" order.rb"字段填充确定,但我无法获得" order_row"要填充的表值。我只是不断在终端中收到以下错误(现在不担心日期,应该没问题)......

未经许可的参数:date(i),order_row

客户模式(customer.rb)......

class Customer < ActiveRecord::Base
  has_many :orders, dependent: :destroy
end

订单型号(order.rb)......

class Order < ActiveRecord::Base
  belongs_to :customer
  has_many :order_rows, dependent: :destroy
  accepts_nested_attributes_for :order_rows
end

Order_Row模型(order_row.rb)

class OrderRow < ActiveRecord::Base
  belongs_to :order
end

(orders_controller.rb)......

def new
  @order = Order.new
end

def create
  @order = Order.new(order_params)

  respond_to do |format|
    if @order.save
      format.html { redirect_to(@order, :notice => 'Order was successfully created.') }
    else
      format.html { render :action => "new" }
    end
  end
end

private
  def order_params
    params.require(:order).permit(:customer_id, :date, :total, 
      :order_row_attributes => [:description, :quantity, :price, :order_id])
  end

new.html.haml上的表单代码

= semantic_form_for @order do |f|
  = f.input :customer_id, :as => :select, :collection => Hash[Customer.all.map{|c|  [c.company,c.id]}]
  = f.input :date
  = f.fields_for :order_row do |ff|
    = ff.input :description
    = ff.input :quantity
    = ff.input :price
    = ff.hidden_field :order_id
  = f.input :total
  = f.action :submit, :as => :button

2 个答案:

答案 0 :(得分:1)

问题在于 order_row_attributes 这一行。它应该是 order_rows_attributes 。如果不允许 date ,请尝试将 date 属性更改为某些名称,例如 order_date

这应该有效

private
  def order_params
    params.require(:order).permit(:customer_id, :order_date, :total, 
    :order_rows_attributes => [:description, :quantity, :price, :order_id])
  end

答案 1 :(得分:0)

我通过将新方法更改为....

来实现它
def new
  @order = Order.new
  @order.order_rows.build
end

所以这个和Pavans的回答结合起来了。