回形针图像未保存

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" 等类型。你能提供你试图保存的图像吗?

很难提出这样的解决方案。 将 create 更改为 create! 以引发异常,然后您可以更好地调试。

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

你也可以试试去掉styles键,看看是不是生成其他图片有问题,可能ImageMagick没有安装。

问题已解决。我不得不升级 ImageMagick。谢谢大家