Question

I have 3 models :

  • User
  • Dish
  • Dish_Image

and I am using devise, paperclip and aws s3 to create users and attach images to the dishes.

Question : The dish is getting created and associated correctly to the user, but the image information is not getting inserted into the dish_image table and no errors are seen anywhere.

I have provide snippets of code what I think may be necessary, but please let me know if any more information is needed.


The app/models/user.rb

Class User < ActiveRecord::Base
  has_many :dishes

The app/models/dish.rb

class Dish < ActiveRecord::Base
  belongs_to :user, dependent: :delete
  has_many :dish_images
  accepts_nested_attributes_for :dish_images

The app/models/dish_image.rb

class DishImage < ActiveRecord::Base
  belongs_to :dish, dependent: :delete
  has_attached_file :d_image, styles: {
    thumb: '100x100>',
    square: '200x200#',
    medium: '300x300>'
  }
  # Validate the attached image is image/jpg, image/png, etc
  validates_attachment_content_type :d_image, :content_type => /\Aimage\/.*\Z/
end

My dish_images_controller.rb file for create reads

def create

    @dish = Dish.find(params[:dish_id])
    @dish_image = @dish.dishimages.build(dish_image_params)
    if @dish_image.save!
      flash[:success] = "Image has been uploaded!"
    else
      flash[:notice] = "Image upload did not work!"
    end
  end

private    
    def dish_image_params
      params.require(:dish_image).permit(:d_image)
    end

The view for upload is

%h2 New Dish
= bootstrap_form_for([current_user, @dish], :html => { :multipart => true }) do |f|
  %div
    = f.text_field :title, autofocus: true
  %div
    = f.text_field :desc, autofocus: true
  %div
    = f.fields_for :dish_images do |ff|
      Dish Image:
      = ff.file_field :d_image, hide_label: true
  %div= f.submit "Submit Dish"

and rake routes gives:

user_dish_dishimages     GET    /users/:user_id/dishes/:dish_id/dishimages(.:format)     dishimages#index
                         POST   /users/:user_id/dishes/:dish_id/dishimages(.:format)     dishimages#create
user_dish_dishimage      DELETE /users/:user_id/dishes/:dish_id/dishimages/:id(.:format) dishimages#destroy
Was it helpful?

Solution

Question : The dish is getting created and associated correctly to the user, but the image information is not getting inserted into the dish_image table and no errors are seen anywhere.

Firstly, when the nested form is submitted, the create action of DishesController would be called. In order to save the dish_images records, you would need to whitelist dish_images_attributes in DishesController.

For example:

def dish_params
  params.require(:dish).permit(:title, :desc, dish_images_attributes: [:id, :d_image])
end

NOTE: If you already have dish_params method to whitelist the attributes you would just need to update the arguments passed to permit in it. If your method for whitelisting attributes is named other than dish_params then update it accordingly.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top