[英]paperclip image is not saving
我正在使用回形針gem上載圖像,但是圖像未保存。
這是我的照片模型:
class Photo < ApplicationRecord
belongs_to :room
has_attached_file :image, styles: {medium: '300x300>', thumb: '100x100>'},
:path => ':rails_root/public/images/:id/:style/:filename',
:url => '/images/:id/:style/:filename'
validates_attachment_content_type :image, content_type: /\Aimage\/.*\z/
# do_not_validate_attachment_file_type :image
end
房間型號:
class Room < ApplicationRecord
belongs_to :user
has_many :photos
end
形成:
<%= file_field_tag "images[]", type: :file, multiple: true %>
控制器:
def create
@room = current_user.rooms.build(room_params)
if @room.save
if params[:images]
params[:images].each do |image|
@room.photos.create(image: image)
end
end
@photos = @room.photos
redirect_to edit_room_path(@room), notice: 'Saved...'
else
render :new
end
end
僅節省空間,而不節省照片模型。 我嘗試使用關系保存圖像,但是即使沒有關系圖像也無法保存,但無法正常工作。 誰能幫我?
很難說問題出在哪里。 看起來像您使用回形針has_attached_file方法。 您上傳的圖片可能不符合驗證條件,其類型不像“ image / jpg”,“ image / jpeg”,“ image / png”,“ image / gif”等類型。 您能提供想要保存的圖像嗎?
很難建議這樣的解決方案。 更改創建以創建! 引發異常,然后可以進行更好的調試。
def create
@room = current_user.rooms.build(room_params)
if @room.save
if params[:images]
params[:images].each do |image|
@room.photos.create!(image: image)
end
end
@photos = @room.photos
redirect_to edit_room_path(@room), notice: 'Saved...'
else
render :new
end
end
您也可以嘗試刪除樣式鍵,以查看生成其他圖像是否有問題,也許未安裝ImageMagick。
問題解決了。 我必須升級ImageMagick。 謝謝你們
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.