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.

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:in `create'

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

?

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.

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.

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?

you post your form view please?

Ofcourse:

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

   <div class="field">
      (...hotel fields...)
   </div>

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

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

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

<% end %>

<%end%>

you post your form view please?

Ofcourse:

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

<div class="field">
(...hotel fields...)
</div>

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

Since you have has_many hphotos you need fields_for :hphotos

Colin

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

Colin

Indeed Colin. Also, I 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