Rails 4 and Active Admin: ActiveModel::ForbiddenAttributesError
Asked Answered
O

2

5

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.

Overbalance answered 20/3, 2014 at 0:27 Comment(0)
O
13

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

Overbalance answered 20/3, 2014 at 3:13 Comment(1)
Actually ":list, :of, :attributes, :on, :model" is just the sample code, we do not need them :)Autograph
L
1

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

Limiting answered 12/8, 2014 at 19:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.