вид:Polymorpic скрепка работает странно с 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
Я хотел полиморфный модель изображения. Я решил заниматься полиморфной картинной ассоциацией, но ее просто откат всегда ... Застрял. Я добавил изображение с консоли. Благодаря!! Debugged it using binding.pry
Знаете ли вы, почему ROLLBACK происходит? – emaillenin
Не могли бы вы сделать журнал 'кухня' перед методом' kitchen.save'. Попробуйте также захватить ассоциацию пользователей и изображений. Это может помочь нам понять. Попробуйте logging '@ kitchen.errors', а также – Surya
@emaillenin, не давая мне никаких ошибок. Не могу понять это. –