繁体   English   中英

回形针图像未保存

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM