【问题标题】:Rails 4 - Paperclip is not uploading filesRails 4 - 回形针不上传文件
【发布时间】:2015-12-11 19:55:24
【问题描述】:

我正在 localhost 中测试一个使用 Paperclip 的 Rails 4 应用程序,即使提交图像会在数据库中创建条目,但图像文件夹始终是空的。

型号

class Tile < ActiveRecord::Base
    belongs_to :game

    # Paperclip
    has_attached_file :image,
        styles: { medium: "300x300>", thumb: "100x100>" },
        url: "/images/:style/:filename",
        default_url: "/images/:style/missing.png"
    validates_attachment_content_type :image, content_type: /\Aimage\/.*\Z/
end

控制器

class TilesController < ApplicationController

    before_action :set_game

    def create
        tile = @game.tiles.create tile_params
        redirect_to @game, notice: tile
    end

    private
        # Use callbacks to share common setup or constraints between actions.
        def set_game
            @game = Game.find(params[:game_id])
        end

        # Never trust parameters from the scary internet, only allow the white list through.
        def tile_params
            params.require(:tile).permit(:image)
        end
end

查看表单

<%= form_for([@game, Tile.new], multipart: true) do |form| %>
    <%= form.file_field :image %>
<% end %>

每次我尝试打印&lt;p&gt;&lt;%= tile.image.url %&gt;&lt;/p&gt;,我都会得到/images/original/missing.png

这是为什么?

更新

  • 我使用的是 Ubuntu 14.04。
  • Imagemagick 已安装。

【问题讨论】:

  • 请显示您的控制器创建/更新操作和表单视图
  • 嗨,如果你只是按照这个,你会没事的:github.com/thoughtbot/paperclip
  • 您的视图可能看起来像这样:
  • 我添加了控制器
  • 能否也为表单添加视图?

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


【解决方案1】:

请务必同时将path 更新为url,即

has_attached_file :image,
    styles: { medium: "300x300>", thumb: "100x100>" },
    path: "/images/:style/:filename",
    url: "/images/:style/:filename",
    default_url: "/images/:style/missing.png"

this Railscasts ~5:20 中所述,url 设置从哪里检索图像,path 设置存储位置。

【讨论】:

  • 你是对的,但这不是作者现在遇到的当前问题 - url 为 ...missing 清楚地表明文件根本没有存储
  • 嗯,这是真的 - 我会等到他按照您在评论中的要求发布他的控制器/视图,但同时保留这个,因为他可能也想在某些时候修复它点。
  • 我添加了控制器
【解决方案2】:

假设您使用window,在屏幕上显示图像:image_tag(tile.image.url(:small))

另外,你安装ImageMagick and file for Windown?'

如果是,您是否将环境设置为:Paperclip.options[:command_path] = 'C:\Program Files (x86)\GnuWin32\bin' 中的config/environments/development.rb?然后重启你的服务器。

【讨论】:

    猜你喜欢
    • 2018-05-06
    • 1970-01-01
    • 1970-01-01
    • 2012-06-10
    • 1970-01-01
    • 2014-04-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多