Reputation: 297
Hi I'm trying to create two object from different models in one form. They are connected by a has_many/belongs_to relationship. The event has_many pictures wich is polymorphic because can be associated with others models. I'm getting Can't mass-assign protected attributes: picture if I set :picture in attr_accesible it's saying me "unknown attribute: picture".
Here is my form code :
<%= form_for @event, :html => { :class => 'form-horizontal' } do |f| %>
<div class="control-group">
<%= f.label :titre, :class => 'control-label' %>
<div class="controls">
<%= f.text_field :titre, :class => 'text_field' %>
</div>
</div>
<div class="control-group">
<%= f.label :Date, :class => 'control-label' %>
<div class="controls">
<%= f.date_select :dday, :class => 'date_select', :start_year=>Date.today.year, :end_year=>1905 %>
</div>
</div>
<div class="control-group">
<%= f.label :lieux, :class => 'control-label' %>
<div class="controls">
<%= f.text_field :lieux, :class => 'text_field' %>
</div>
</div>
<div class="control-group">
<%= f.label :description, :class => 'control-label' %>
<div class="controls">
<%= f.text_area :commentaire, :class => 'text_area', :rows => 4 %>
</div>
</div>
<%= f.fields_for @event.pictures.new do |p| %>
<%= p.text_field :name, :placeholder=>"Nom de votre image" %>
<%= p.file_field :image %>
<% end %>
and my models code
class Event < ActiveRecord::Base
attr_accessible :dday, :lieux, :titre, :commentaire, :picture
belongs_to :user
has_many :pictures, :as => :imegeable ,:dependent => :destroy
default_scope :order => :dday
validates :dday, :titre, :presence=>true
end
Thanks for your help.
Upvotes: 0
Views: 307
Reputation: 6921
Try to change last fields_for from your code to:
<%= f.fields_for :pictures do |p| %>
<%= p.text_field :name, :placeholder=>"Nom de votre image" %>
<%= p.file_field :image %>
<% end %>
Add to your model:
accepts_nested_attributes_for :pictures
And change attr_accessible in your model to:
attr_accessible :dday, :lieux, :titre, :commentaire, :pictures_attributes
Upvotes: 0
Reputation: 2469
It's been a while since I've done any Rails work, so this might've changed since I last did it, but I think you need to specify that accepts_nested_attributes_for :pictures
on your Event model.
Upvotes: 1