Undefined method `tripphoto_changed? 'for ... Carrierwave

I am trying to save the image when adding a new trip through the association.

tripphoto.rb has belongs_to :trip

and my trip.rb has has_many :tripphotos . In addition, the Carrierwave documentation requires a few more things, such as the bootloader and those that I configured correctly (it worked without association!).

Along with this, I have mount_uploader :tripphoto, TripphotoUploader in my trip.rb and accepts_nested_attributes_for :tripphotos, allow_destroy: true so that my form works correctly:

  <%= fields_for :tripphotos do |photo| %> Img <%= photo.file_field :filename %> <% end %> 

However, I get an undefined method tripphoto_changed error message? 'for # `. Fast Google does not have a suitable result. Any tips?

PS here is my Tripphoto table:

 class Tripphoto < ActiveRecord::Base { :id => :integer, :filename => :string, :trip_id => :integer, :created_at => :datetime, :updated_at => :datetime } 

Thanks in advance,

Edit: on request my codes are:

Trip.rb

 class Trip < ActiveRecord::Base attr_accessible :description, :title, :user_id, :triplocations_attributes, :photo, :category_ids, :start_city, :tripphotos, :img_url, :thumb_url, :images, :tags_attributes # validates :title, :length => {:minimum => 3} # validates :description, :presence => true # validates_associated :tags has_many :triplocations, :dependent => :destroy has_many :tripphotos, :dependent => :destroy has_and_belongs_to_many :categories has_and_belongs_to_many :tags accepts_nested_attributes_for :triplocations, allow_destroy: true accepts_nested_attributes_for :tripphotos, allow_destroy: true accepts_nested_attributes_for :categories accepts_nested_attributes_for :tags mount_uploader :tripphoto, TripphotoUploader end 

Tripphoto.rb

 class Tripphoto < ActiveRecord::Base attr_accessible :filename, :trip_id belongs_to :trip end 

Full travel form

 <%= form_for @trip, :html => {:multipart => true} do |a| %> <%= a.label :title, "Routetitel" %> <%= a.text_field :title %> <%= a.label :description, "Omschrijving" %> <%= a.text_area :description %> <%= a.label :start_city, "Stad" %> <%= a.text_field :start_city, :id => "cityName" %> <% for category in Category.find(:all) %> <div> <%= check_box_tag "trip[category_ids][]", category.id, @trip.categories.include?(category) %> <%= category.name %> </div> <% end %> <%= a.fields_for :tags do |f| %> <p> <%= f.label :title, 'Steekwoorden:' %> <%= f.text_field :title %> </p> <div id="inputFields"></div> <% end %> <div style="background: red;"> <%= fields_for :tripphotos do |photo| %> Img <%= photo.file_field :filename %> <% end %> </div> <%= a.submit 'Verstuur' %> <% end %> 
+4
source share
3 answers

Two issues here:

  • you must install the bootloader in the field where the file name will be stored, the filename field in the Tripphoto model in this case, and not the Tripphoto association.
  • you forgot a. before fields_for , look what you did for the tags
+3
source

I had the same problem

For me it was that I forgot to run:

 bundle exec rake db:migrate 

Is that the reason for youruploaderattributte_changed? Missing method in moodel.

+6
source

Same issue caused by a little stupid mistake. First i run

 rails generate uploader Photo 

and install

 mount_uploader :photo, PhotoUploader 

in the model. But then I used the "avatar" instead of the "photo" in the migration:

 rails g migration add_avatar_to_users avatar:string 

That was stupid! Eh eh eh; -)

+1
source

Source: https://habr.com/ru/post/1443418/


All Articles