Has_Many, another mass assign headache

I’m using rails 3.2.3 and am having issues with mass assignement.

I have a Hotel model and a Hphoto model (integrated with paperclip)

I have tried almost everything and still get mass-assignement error.
Can’t mass-assign protected attributes: hphoto


Hotel model

has_many :hphotos, :dependent=>:destroy
accepts_nested_attributes_for , :hphotos
attr_accessible: , :hphoto_attributes

Hphoto model

belongs_to :hotel
has_attached_file :photo,
:path => “:rails_root/public/system/:attachment/:id/:style/:filename”,
:url => “/system/:attachment/:id/:style/:filename”

attr_accessible :hphoto_attributes

validates_attachment_presence :photo
validates_attachment_size :photo, :less_than => 2.megabytes
validates_attachment_content_type :photo, :content_type=> [‘image/jpeg’,
‘image/png’]

My hotel controller:

def new
@hotel = Hotel.new
@hotel.hphotos.build

respond_to do |format|
  format.html
  format.json { render :json => @hotel }
end

end

def create
@hotel = Hotel.new(params[:hotel])

end


Thanks for your thoughts, this seems so simple yet I can’t fix it

On 9 May 2012 16:42, Miguel A. [email protected] wrote:

I’m using rails 3.2.3 and am having issues with mass assignement.

I have a Hotel model and a Hphoto model (integrated with paperclip)

I have tried almost everything and still get mass-assignement error.

What’s the actual error that you get?

What’s the actual error that you get?

Can’t mass-assign protected attributes: hphoto
app/controllers/hotels_controller.rb:52:in new' app/controllers/hotels_controller.rb:52:increate’

On 9 May 2012 16:56, Miguel A. [email protected] wrote:

What’s the actual error that you get?

Can’t mass-assign protected attributes: hphoto
app/controllers/hotels_controller.rb:52:in new' app/controllers/hotels_controller.rb:52:in create’

Have you tried
attr_accessible :hphoto
rather than (or in addition to)
attr_accessible :hphoto_attributes
?

On 9 May 2012 17:15, Miguel A. [email protected] wrote:

If I also add to the Hotel model:
attr_accessible :hphoto, :hphoto_attributes
it says “unknown attribute: hphoto”
if i take it out and leave just :hphoto_attributes, the error is still
the original one.

Sorry, I misread your initial post.

OK, I’m guessing your form is sending some data in a :hphoto hash,
rather
than a :hphoto_attributes hash. That would explain the inconsistency.
Can
you post your form view please?

Have you tried
attr_accessible :hphoto
rather than (or in addition to)
attr_accessible :hphoto_attributes
?

If i add to the Hphoto model:
attr_accessible :hphoto, :hphoto_attributes
the result is the same.

If I also add to the Hotel model:
attr_accessible :hphoto, :hphoto_attributes
it says “unknown attribute: hphoto”
if i take it out and leave just :hphoto_attributes, the error is still
the original one.

you post your form view please?
Ofcourse:

<%= form_for @hotel, :html => {:multipart => true} do |f| %>

(...hotel fields...)

<%= f.fields_for :hphoto do |builder| %>

<%= builder.label :description, "Photo Description: " %> <%= builder.text_field :description %>

<%= builder.label :photo, "Image File: " %> <%= builder.file_field :photo %>

<% end %>

<%end%>

<%= f.fields_for :hphoto do |builder| %>
Since you have has_many hphotos you need fields_for :hphotos

Colin

Indeed Colin. Also, I also missed an attr_accessible when I was
testing/modifying code. The :hphoto in the Hphoto model.

You’ve already answered 2 of my questions with positive results, many
thanks for dropping by ruby-forum so frequently.

Regards

On 9 May 2012 17:39, Miguel A. [email protected] wrote:

<%= f.fields_for :hphoto do |builder| %>
Since you have has_many hphotos you need fields_for :hphotos

Colin