I need to create revisions for products, so I have to move editable information in different table
I created nested form, but having troubles saving data: ActiveModel::ForbiddenAttributesError
As I understand, the problem is because of the naming, but I tried to call it product_infos, product_infos_attributes, etc... nothing is working
I also tried to change the behavior, by changing
config.action_controller.action_on_unpermitted_parameters = false
but I'm still getting this exception
it works fine if I will change .create to .new and will assign all data manually, but there are a lot of fields, I'd rather use mass assignment
is there a way to get the name of the field(s) which cause the problem?
Code:
controller:
def create
product = Product.new(product_params)
if product.save params[:product].merge(:user_id => current_user.id)
flash[:success] = t('msg.saved')
redirect_to product_path(product)
else
render 'new'
end
end
def product_params
params[:product][:slug] = make_slug(params[:product][:product_info][:caption])
params.require(:product).permit(:image, product_infos: [:caption, :description])
end
model (product):
class Product < ActiveRecord::Base
has_many :product_infos
accepts_nested_attributes_for :product_infos
def save(params)
self.user_id = params[:user_id]
self.slug = params[:slug]
if super
# I have this error here
revision = self.product_infos.create(params[:product_info])
params[:image].each do |file|
self.product_images.create(:image => file)
end
true
end
end
end
model (product_info):
class ProductInfo < ActiveRecord::Base
belongs_to :product
end
form:
<%= form_for @product, :html => {:multipart => true} do |f| %>
<%= f.fields_for :product_info do |i| %>
<%= i.label :caption %>
<%= i.text_field :caption, class: 'form-control', required: 'required' %>
<% end %>
<% end %>
Error:
ActiveModel::ForbiddenAttributesError in ProductsController#create
ActiveModel::ForbiddenAttributesError
Extracted source (around line #57):
55
56
57
58
59
60
if super
revision = self.product_infos.new(params[:product_info])
revision.caption = params[:product_info][:caption]
revision.coordinates = params[:product_info][:coordinates]
revision.user_id = params[:user_id]
Rails.root: /Users/lasoweq/Sites/ruby/eshop
Application Trace | Framework Trace | Full Trace
app/models/product.rb:57:in 'save'
app/controllers/products_controller.rb:22:in 'create'
Request
Parameters:
{"utf8"=>"✓",
"authenticity_token"=>"Iwg5hdVB+5aAt+Mij8Tg7mMJv+lUr1RXzKciwRVC/ATmtU1FH3odLNaNDhssWz8vbUK+YSTvT0ErB+3cAI5/Tg==",
"product"=>{"product_info"=>{"caption"=>"Test123",
"coordinates"=>"13.727685539497559,
100.63547114393305",
"description"=>"<p>adasdasdasd</p>",
"sizes"=>"asdasdasd",
"height"=>"asdasd",
"admission"=>"zxczxcz",
"free_admission"=>"0",
"custom_info"=>"zxczxczxc"},
"categories"=>["",
"3"],
"image"=>[#<ActionDispatch::Http::UploadedFile:0x007fd529922c18 @tempfile=#<Tempfile:/var/folders/yt/dtw20h393hvd4xfwcjzhcqxr0000gn/T/RackMultipart20150623-76404-c0nkwa.jpg>,
@original_filename="GrizzlyBear1920x12005-23-2011_12_51_12_PM.jpg",
@content_type="image/jpeg",
@headers="Content-Disposition: form-data; name=\"product[image][]\"; filename=\"GrizzlyBear1920x12005-23-2011_12_51_12_PM.jpg\"\r\nContent-Type: image/jpeg\r\n">,
#<ActionDispatch::Http::UploadedFile:0x007fd529922ab0 @tempfile=#<Tempfile:/var/folders/yt/dtw20h393hvd4xfwcjzhcqxr0000gn/T/RackMultipart20150623-76404-1si889i.jpg>,
@original_filename="lion.jpg",
@content_type="image/jpeg",
@headers="Content-Disposition: form-data; name=\"product[image][]\"; filename=\"lion.jpg\"\r\nContent-Type: image/jpeg\r\n">]},
"commit"=>"Сохранить"}
You have a
product
withhas_many
relation toproduct_infos
,so this lineshould be
And also you have to change
:product_infos
to:prodcut_infos_attributes
in yourproduct_params
method