简体   繁体   English

回形针图像未保存

[英]paperclip image is not saving

I am using paperclip gem to upload the images but the image is not saving. 我正在使用回形针gem上载图像,但是图像未保存。

Here is my Photo model: 这是我的照片模型:

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

Room model: 房间型号:

class Room < ApplicationRecord
    belongs_to :user
    has_many :photos
end

form: 形成:

<%= file_field_tag "images[]", type: :file, multiple: true %>

controller: 控制器:

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

Only room is saving but not the photo model. 仅节省空间,而不节省照片模型。 I tried to save images using relationship but not working even without relationship image is not saving. 我尝试使用关系保存图像,但是即使没有关系图像也无法保存,但无法正常工作。 Can anyone help me? 谁能帮我?

Hard to say where exactly problem lies. 很难说问题出在哪里。 Looks like u using paperclip has_attached_file method. 看起来像您使用回形针has_attached_file方法。 Probably your uploaded image does not meet validation and its type isnt like something "image/jpg", "image/jpeg", "image/png", "image/gif" etc types. 您上传的图片可能不符合验证条件,其类型不像“ image / jpg”,“ image / jpeg”,“ image / png”,“ image / gif”等类型。 Can u provide what image u trying to save. 您能提供想要保存的图像吗?

It's hard to suggest to a solution like this. 很难建议这样的解决方案。 Change create to create! 更改创建创建! to raise an exception and then you can debug better. 引发异常,然后可以进行更好的调试。

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

You can also try removing styles key to see if something is wrong with generating other images, maybe ImageMagick is not installed. 您也可以尝试删除样式键,以查看生成其他图像是否有问题,也许未安装ImageMagick。

Problem solved. 问题解决了。 I had to upgrade ImageMagick. 我必须升级ImageMagick。 Thank you all 谢谢你们

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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