简体   繁体   中英

Polymorpic paperclip working strange with Rails 5

view:

<div class="kitchen_settings">
<%= form_for @kitchen, :html => { :multipart => true, "data-ajax" => false} do |f| %>
     <%= f.text_field :title,placeholder: "#{current_user.fullname}'s Kitchen", autofocus: true %>
    <%= f.text_field :bio, placeholder: 'something about your kitchen' %>

    <%= f.fields_for :picture do |photo| %>
        <%= photo.file_field :avatar %>
    <% end %>
    <%= f.submit %>
<% end %>

kitchen_controller.rb

class KitchensController < ApplicationController
    before_action :authenticate_user!

    def new
        @kitchen = Kitchen.new
        @kitchen.build_picture
    end

    def create
        @kitchen = current_user.build_kitchen(kitchen_params)
    respond_to do |format|
        if @kitchen.save
          format.html { redirect_to dashboard_path, notice: 'Kitchen was successfully created.' }
          format.json { render :show, status: :created, location: dashboard_path }
        else
          format.html { render :new }
          format.json { render json: @kitchen.errors, status: :unprocessable_entity }
        end
      end
    end

    def show
        @kitchen = Kitchen.find (params[:id])
    end

    private
    def kitchen_params
        params.require(:kitchen).permit(:title,:bio, picture_attributes: [:avatar])
    end
end

kitchen.rb

class Kitchen < ApplicationRecord
  belongs_to :user

  has_one :picture, as: :imageable, dependent: :destroy
  accepts_nested_attributes_for :picture
end

picture.rb

class Picture < ApplicationRecord
    belongs_to :imageable, polymorphic: true

    has_attached_file :avatar, styles: { medium: "300x300>", thumb: "100x100"}, default_url: "/assets/:style/missing.png"
  validates_attachment :avatar, :presence => true,
    :content_type => { :content_type => ["image/jpeg", "image/gif", "image/png"] },
    :size => { :in => 0..500.kilobytes }
end

And its giving me this error

I wanted polymorphic picture model. I decided to deal with polymorphic picture association but its just rollbacks always... Stuck. I've attached the image from the console. Thanks!! Debugged it using binding.pry

In Rails 5, whenever we define a belongs_to association, it is required to have the associated record present by default after this change.To change this behavior i managed to do it this way:

picture.rb

class Picture < ApplicationRecord
    belongs_to :imageable, polymorphic: true, optional: true

    has_attached_file :avatar, styles: { medium: "300x300>", thumb: "100x100"}, default_url: "/assets/:style/missing.png"

  validates_attachment :avatar, :presence => true,
    :content_type => { :content_type => ["image/jpeg", "image/gif", "image/png"] },
    :size => { :in => 0..500.kilobytes }
end

There is a problem with you validation of imageable in image.rb

When you store kitchen alongwith image in single go. Imageable validation is failed for image as kitchen is not created yet. That's why it's rollback.

To confirm it you can remove the validation temporarily.

You need to use inverse_of to make this work

in image.rb

belongs_to :imageable, polymorphic: true , inverse_of: :image

in kitchen.rb

has_one :picture, as: :imageable, dependent: :destroy,inverse_of: :imageable

I added inverse_of in both the associations. It tells rails that these associations are inverse to each other so if any of it's set then don't make a query to fetch the other.

In this way if you set any association then other will be set automatically and validation won't fail.

Here is a nice blog for inverse_of usage.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM