【问题标题】:Rails Paperclip images not showing up in on cloud9 serverRails Paperclip 图像未显示在 cloud9 服务器上
【发布时间】:2016-06-08 19:33:11
【问题描述】:

我正在使用回形针将图像上传到 cloud9 虚拟环境 (ubuntu) 上的 rails 应用程序。有一个电影模型,每个电影对象都有一个与之关联的图像。

class Movie < ActiveRecord::Base
    belongs_to :user
    belongs_to :category
    has_attached_file :movie_img, styles: { medium: "250x350>", thumb: "325x475>" }, default_url: "/images/style/missing.png"
    validates_attachment_content_type :movie_img, content_type: '/\Aimage\/.*\Z/'
end

视图是这样的

    <% @movies.each do |movie| %>
            <%= image_tag movie.movie_img.url(:movie_index), class: "movie" %>
    <% end %>

但是图片没有显示在网站上。

生成的html中图片的路径是这样的

系统/电影/movie_imgs/000/000/010/movie_index/fileName.jpg?1465401579 正如我从 Cloud9 的文件资源管理器中看到的那样,图像存在于实际目录中。

当我打开这个图片地址时,它显示以下错误。

没有路线匹配 [GET] "/system/movies/movie_imgs/000/000/011/movie_index/fileName.png"

同样在 config/environments/development.rb 我写了下面这行代码

  Paperclip.options[:command_path] = "/usr/bin/"

因为当我在终端中输入 which convert 时,会出现以下内容。

/usr/bin/转换

我哪里错了?如何让图片出现在服务器中?

编辑 我还生成了必要的迁移,我的电影表看起来像这样。

create_table "movies", force: :cascade do |t|
    t.string   "title"
    t.text     "description"
    t.string   "director"
    t.date     "release_date"
    t.datetime "created_at",             null: false
    t.datetime "updated_at",             null: false
    t.integer  "user_id"
    t.integer  "category_id"
    t.string   "movie_img_file_name"
    t.string   "movie_img_content_type"
    t.integer  "movie_img_file_size"
    t.datetime "movie_img_updated_at"

结束

没有显示静止图像。

【问题讨论】:

    标签: ruby-on-rails ruby ruby-on-rails-4 paperclip


    【解决方案1】:

    您似乎正在声明一个视图助手来调整未在电影模型中定义的图像 (:movie_index) 的大小。

    您需要在模型中声明 helper 和 :movie_index 的大小:

    class Movie < ActiveRecord::Base
        belongs_to :user
        belongs_to :category
        has_attached_file :movie_img, styles: { movie_index: "300x300>" medium: "250x350>", thumb: "325x475>" }, default_url: "/images/style/missing.png"
        validates_attachment_content_type :movie_img, content_type: '/\Aimage\/.*\Z/'
    end
    

    编辑:

    确保您已经生成了必要的迁移:

    rails g paperclip Movie movie_img
    

    请务必阅读Paperclip documentation

    【讨论】:

    • 我已经生成了必要的迁移,并在问题详细信息中添加了我的架构。请检查一次。图像存在于该路径中,但它们在站点中不可见。
    • @YashSharma 您是否按照我的建议添加了“movie_index: "300x300>"?
    • 非常感谢。这解决了问题。非常感谢!!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-05-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多