我无法弄清楚如何创建必须附加一张图片的事件,图片是多态的(由于应用程序的其余部分)。
(逐步我想要的。)
目前我看起来像这样:
picture.rb
class Picture < ApplicationRecord
acts_as_list
belongs_to :imageable, polymorphic: true
has_attached_file :image,
:storage => :s3,
:bucket => Figaro.env.s3_bucket,
:s3_region => 'eu-west-1',
:s3_credentials => {
:access_key_id => Figaro.env.aws_access_key_id,
:secret_access_key => Figaro.env.aws_secret_access_key
}
do_not_validate_attachment_file_type :image
end
event.rb
class Event < ApplicationRecord
## Associaties
has_many :pictures, -> { order(position: :asc) }, as: :imageable, dependent: :destroy
## Validaties
validates_presence_of :name, :description, :date, :pictures
validates :name, length: { maximum: 100 }
validates :description, length: { maximum: 500 }
validates :date, in_future: true
end
events_controller.rb
def create
@event = Event.new(event_params)
authorize @event
if @event.save
if params[:image]
@event.pictures.create(image: params[:image])
end
flash[:notice] = 'Agendapunt is toegevoegd.'
redirect_to events_path
else
flash.now[:alert] = 'Agendapunt is niet toegevoegd.'
render 'new'
end
end
它目前一直在给我一个&#34;图片不能出现空白错误&#34;。
我记得有一段时间出现逆问题,即如何确保我必须上传图片。现在我无法弄清楚如何修复图像的错误,而不是通过某种方式。