多功能回形针与Rails 5工作奇怪

时间:2017-02-14 00:13:12

标签: ruby-on-rails ruby paperclip ruby-on-rails-5

查看:

<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

我想要多态图片模型。我决定处理多态图片关联,但它只是回滚总是......坚持。我已经从控制台附加了图像。谢谢!! Debugged it using binding.pry

2 个答案:

答案 0 :(得分:1)

在Rails 5中,每当我们定义belongs_to关联时,都需要在此更改后默认存在关联记录。要改变此行为,我设法这样做:

<强> 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

答案 1 :(得分:0)

您在imageable

中验证image.rb时出现问题

当您将厨房与图像一起存放时。 Imageable验证图片失败,因为尚未创建厨房。这就是它回滚的原因。

要确认,您可以暂时删除验证。

您需要使用inverse_of来完成这项工作

image.rb

中的

belongs_to :imageable, polymorphic: true , inverse_of: :image
kitchen.rb

中的

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

我在两个关联中添加了 inverse_of 。它告诉rails这些关联是彼此相反的,所以如果它的任何设置然后不进行查询以获取另一个。

这样,如果您设置了任何关联,则会自动设置其他关联,并且验证不会失败。

Here是一个很好的反向使用博客。