polarcare
polarcare

Reputation: 575

Unpermitted parameters - Rails 4.1.1, Ruby 2.1.2

I cannot seem to get to the bottom of where I am going wrong. My "order.rb" fields populate ok, but I can't get the "order_row" table values to populate. I just keep getting the following error in terminal(not worried about date for now, should be ok with that)...

Unpermitted parameters: date(i), order_row

Customer model(customer.rb)...

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

Order model(order.rb)...

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

Order_Row model(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

Form code on 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

Upvotes: 0

Views: 297

Answers (2)

polarcare
polarcare

Reputation: 575

I got it working by changing the new method to....

def new
  @order = Order.new
  @order.order_rows.build
end

So combination of this and Pavans answer did the trick.

Upvotes: 0

Pavan
Pavan

Reputation: 33552

The problem is this line order_row_attributes.It should be order_rows_attributes. And with the date not being permitted,try changing the date attribute to some name like order_date.

This should work

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

Upvotes: 1

Related Questions