Polymorphic association - undefined method `image_data'

195 views
Skip to first unread message

rss...@gmail.com

unread,
Aug 18, 2017, 9:50:37 AM8/18/17
to Shrine


Hello Everyone,

I am learning rails as I develop a prototype app. It has been a great journey so far, at least until I got into file uploads a few days ago.

Multiple models in the app need to save one or more images, so I decided to use polymorphic associations.

Until I understand how to use direct_upload for local storage (I struggle to find proper guidance on this that includes javascript), I decided to stick to a single image per model record.


I am getting the undefined method `image_data' when I submit the form. The Image Uploader is being called when the :image param is seen as confirmed by the creation of a cached file (STORE[cache] ImageUploader[:image] Client 1 file (0.03s)).

The error appears to occur when the model object is created.


I am not expert, but one reason I am thinking of is that Shrine is trying to access the image_data method in the client model, which is not available. I am saying this because the client object (but not the associated image record) saves successfully is I add "attr_accessor :image_data" in the client model file.

Thank you all in advance for your assistance.

Luca 


Luca Rossi

unread,
Aug 19, 2017, 2:56:45 AM8/19/17
to Shrine
client.rb
class Client < ApplicationRecord
  belongs_to :business
  belongs_to :created_by, :class_name => "User"
  belongs_to :updated_by, :class_name => "User"
  has_one :location, through: :updated_by
  
  has_many :images, as: :imageable, dependent: :destroy
  accepts_nested_attributes_for :images, allow_destroy: true

  include ImageUploader[:image]

image.rb
class Image < ApplicationRecord
    belongs_to :imageable, polymorphic: true
end

clients_controller.rb
  def create

    # Add business, creator and location params from session. Also updated_by is saved with the creator id
    @client = Client.new(client_params.merge(:business_id => current_user.business_id, :created_by_id => current_user.id, :updated_by_id => current_user.id,))

    respond_to do |format|
      if @client.save
        format.html { redirect_to edit_client_path(@client), notice: 'New client successfully created.' }
        format.json { render :shot, status: :created, location: @client }
      else
        format.html { render :new }
        format.json { render json: @client.errors, status: :unprocessable_entity }
      end
    end

  end
[...]
    # Never trust parameters from the scary internet, only allow the white list through.
    def client_params
      params.require(:client).permit(:first_name, 
                                      :last_name, 
                                      :dob, 
                                      :sex, 
                                      :address, 
                                      :suburb, 
                                      :state, 
                                      :postcode, 
                                      :email, 
                                      :phone, 
                                      :agreement,
                                      :image,
                                      )
    end


Luca Rossi

unread,
Aug 19, 2017, 6:05:32 AM8/19/17
to Shrine

finally.. optional: true is the answer !

class Image < ApplicationRecord
    include ImageUploader[:image]
    belongs_to :imageable, polymorphic: true, optional: true
end

 
Reply all
Reply to author
Forward
0 new messages