回形针上传并显示图像不起作用

时间:2017-11-09 18:19:08

标签: ruby-on-rails ruby imagemagick paperclip

我安装了回形针 - > 5.1和Windows 7上的最新ImageMagick(是的......我知道)运行Ruby 2.3.3和Rails 5.1.4。

我按照回形针说明的建议安装了file.exe,并将“C:\ Program Files(x86)\ GnuWin32 \ bin”添加到了我的路径中。

当我上传图片时,我收到以下错误:

ActionController::RoutingError (No route matches [GET] "/images/medium/missing.png"):

当我尝试查看显示页面以查看图像时,我收到以下错误:

ActionController::RoutingError (No route matches [GET] "/system/recipes/images/000/000/005/medium/Capture777.PNG"):

您能否就如何正确上传图片并进行展示给我指导?我假设错误会给出一些输入。我试图关注其他SO文章但到目前为止没有任何帮助。

这是我的routes.rb

Rails.application.routes.draw do

  resources :recipes

  root "recipes#index"
end

这是我的控制器show / new actions和find_recipe params

def show
  @recipe = find_recipe
end

def new
  @recipe = Recipe.new
end

private

def find_recipe
  @recipe = Recipe.find(params[:id])
end

def recipe_params
  params.require(:recipe).permit(:title, :description, :image)
end

这是我的模特

class Recipe < ApplicationRecord
  has_attached_file :image, style: { :medium => "400x400#" }
  validates_attachment_content_type :image, content_type: /\Aimage\/.*\z/
end

这是我的show.html.haml

= image_tag @recipe.image.url(:medium, class: "recipe_image")
%h1= @recipe.title
%p= @recipe.description
= link_to 'Back', root_path, class: 'btn btn-info'
= link_to 'Edit', edit_recipe_path, class: 'btn btn-primary'
= link_to 'Delete',recipe_path, method: :delete, data: { confirm: 'Are you sure?' }, class: 'btn btn-danger'

这是我的_form.html.haml

= simple_form_for @recipe, html: { multipart: true } do |f|
- if @recipe.errors.any?
#errors
  %p
    = @recipe.errors.count
    Prevented this recipe from saving
  %ul
    - @recipe.errors.full_messages.each do |msg|
      %li = msg
.panel-body
= f.input :title, input_html: { class: 'form-control' }
= f.input :description, input_html: { class: 'form-control' }
= f.file_field :image, input_html: { class: 'form-control' }


= f.button :submit, class: 'btn btn-primary'

1 个答案:

答案 0 :(得分:1)

问题在于您尝试使用样式选项的方式,它必须是styles,替换它并重试:

class Recipe < ApplicationRecord
  has_attached_file :image, styles: { medium: '400x400#' }
  validates_attachment_content_type :image, content_type: /\Aimage\/.*\z/
end

这使得Paperclip只执行命令:

  • Command :: file -b --mime开始交易
  • Command :: file -b --mime创建副本

代替:

  • Command :: file -b --mime
  • Command :: identify -format
  • Command :: identify -format %m
  • Command :: convert
  • Command :: file -b --mime

因此,您无法通过:medium样式获取图像,但您可以访问其网址并通过图片标记显示该图像,但样式将无效。