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. --------------------------------------------------------------- Hotel model has_many :hphotos, :dependent=>:destroy accepts_nested_attributes_for <other models>, :hphotos attr_accessible: <other attributes>, :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]) <original scaffold code> end --------------------------------------------------------------- Thanks for your thoughts, this seems so simple yet I can''t fix it -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 9 May 2012 16:42, Miguel A. <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> 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?> > --------------------------------------------------------------- > Hotel model > > has_many :hphotos, :dependent=>:destroy > accepts_nested_attributes_for <other models>, :hphotos > attr_accessible: <other attributes>, :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]) > <original scaffold code> > end > > --------------------------------------------------------------- > > Thanks for your thoughts, this seems so simple yet I can''t fix it > > -- > Posted via http://www.ruby-forum.com/. > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
> 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'' -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 9 May 2012 16:56, Miguel A. <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> 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 ?> -- > Posted via http://www.ruby-forum.com/. > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
> 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. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 9 May 2012 17:15, Miguel A. <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> > 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?> -- > Posted via http://www.ruby-forum.com/. > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
> 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%> -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 9 May 2012 17:39, Miguel A. <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:>> 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> > <p> > <%= builder.label :description, "Photo Description: " %> > <%= builder.text_field :description %> > </p> > > <p> > <%= builder.label :photo, "Image File: " %> > <%= builder.file_field :photo %> > </p> > > <% end %> > > <%end%> > > -- > Posted via http://www.ruby-forum.com/. > > -- > You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
> <%= f.fields_for :hphoto do |builder| %> > Since you have has_many hphotos you need fields_for :hphotos > > ColinIndeed 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 -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.