Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 4 and Active Admin: ActiveModel::ForbiddenAttributesError

The full error is the following:

ActiveModel::ForbiddenAttributesError in Admin::ProductsController#create

My product model only has a name and price. Why is commit a parameter? When I click the 'Create Product' button within the admin dashboard, this is the params output:

Parameters:

{"utf8"=>"✓",
 "authenticity_token"=>"6/pCeklsaik4sYF5h8+WRPddkH7wJn9ZJHd6YLaaNuc=",
 "product"=>{"name"=>"Black Shirt Male",
 "price"=>"25"},
 "commit"=>"Create Product"}

From what I've gathered reading other Stack Overflow posts, you need to use strong parameters in Rails 4 instead of attr_accessible, which was done for me when I scaffolded the product model. In my create action in the Products controller, I have:

@product = Product.new(product_params)

And product_params is defined as:

def product_params
  params.require(:product).permit(:name, :price)
end

I didn't do anything fancy when I created the model, and in my Gemfile I'm using the following as the documentation suggested for Rails 4:

gem 'stripe', :git => 'https://github.com/stripe/stripe-ruby'

Why am I getting this error when I create a new product via the Active Admin dashboard? Any input on the matter is appreciated.

like image 645
Nubtacular Avatar asked Mar 20 '14 00:03

Nubtacular


2 Answers

Alright figured it out. I'm not sure if this is the 'correct' way but the products are being created.

in app/admin/product.rb I did:

permit_params :list, :of, :attributes, :on, :model, :name, :price

where

permit_params :list, :of, :attributes, :on, :model

was initially commented out. So I just added :name and :price

like image 164
Nubtacular Avatar answered Nov 03 '22 08:11

Nubtacular


Question already answered, but I'm adding this as a helpful resource to supplement this answer:

https://github.com/activeadmin/active_admin/blob/master/docs/2-resource-customization.md#setting-up-strong-parameters

like image 22
etusm Avatar answered Nov 03 '22 08:11

etusm