【问题标题】:Rails 4 Paperclip Images not uploading/showing (only missing.png)Rails 4 Paperclip 图片未上传/显示(仅缺少.png)
【发布时间】:2014-09-15 22:34:34
【问题描述】:

首先,这些是我的环境版本: 导轨:4.1.0 红宝石:2.1.1p76 回形针:4.1

我创建了一个脚手架(rails g scaffold Entry description:text)并进一步将 Paperclip 添加到当时现有的模型中(rails g paperclip entry image)。

之后我迁移了,到目前为止一切正常。 现在,当我上传图片时,它只是不显示,而是显示“/images/original/missing.png”,并且根本没有我刚刚上传的图片的记录。

这是我的模型(models/entry.rb):

class Entry < ActiveRecord::Base
  has_attached_file :image,
        :path => ":rails_root/public/images/:class/:attachement/:id/:basename.:extension",
        :url => "/images/:class/:attachement/:id/:basename.:extension"

end

我的观点(show.html.slim):

p#notice = notice

p
  strong Description:
  = @entry.description
  = image_tag @entry.image.url

= link_to 'Edit', edit_entry_path(@entry)
'|
= link_to 'Back', entries_path

我安装了 ImageMagick,甚至在我的 development.rb 中设置了 Paperclip.options。 我不知道我在这里缺少什么,它似乎没有上传任何图像,也没有抛出任何错误消息。

【问题讨论】:

    标签: ruby-on-rails paperclip


    【解决方案1】:

    检查您的 :path => ":rails_root/public/images/:class/:attachment/:id/:basename.:extension"

    确保可以追溯到图像的存储位置

    【讨论】:

      【解决方案2】:

      经过更多研究和几杯冷饮后,我找到了解决方案!

      有必要明确允许上传某些格式或删除验证检查(我建议在开发中这样做)。 这样做就像在模型中添加以下行一样简单(对我来说,entry.rb) (来源:https://stackoverflow.com/a/21898204/3686898

      do_not_validate_attachment_file_type :image
      

      另外,我在控制器中添加了另一个检查(与早期 Rails 版本中的 attr_accessible 相同):

        private
          # Use callbacks to share common setup or constraints between actions.
          def set_entry
            @entry = Entry.find(params[:id])
          end
      
          # Never trust parameters from the scary internet, only allow the white list through.
          def entry_params
            params.require(:entry).permit(:description, :image)
          end
      

      好的,我希望这可以帮助某人:) (永远记得看看你的服务器日志。它提供了黄金信息!)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-10-20
        • 2013-11-09
        • 2014-01-18
        • 2013-12-29
        • 1970-01-01
        • 1970-01-01
        • 2015-08-11
        相关资源
        最近更新 更多