iceveda06
iceveda06

Reputation: 619

Paperclip upload and showing the image not working

I installed paperclip -> 5.1 and latest ImageMagick on Windows 7(yeah..i know) running Ruby 2.3.3 and Rails 5.1.4.

I installed file.exe as suggested by paperclip instructions and added 'C:\Program Files (x86)\GnuWin32\bin' to my PATH.

When i upload my pictures i get the following error:

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

and when i try to view the show page to see the images i get the following error:

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

Can you give me guidance on how to properly upload my image and show it? I'm assuming the errors would give some input. I tried to follow other SO articles but nothing helped so far.


Here is my routes.rb


Rails.application.routes.draw do

  resources :recipes

  root "recipes#index"
end

Here is my controller show/new actions and 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

Here is my Model


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

Here is my 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'

Here is my _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'

Upvotes: 1

Views: 418

Answers (1)

Sebasti&#225;n Palma
Sebasti&#225;n Palma

Reputation: 33420

The problem is the way you're trying to use the styles option, it must be styles, replace it and try again:

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

This makes Paperclip only execute the commands:

  • Command :: file -b --mime to start the transaction
  • Command :: file -b --mime to create a copy

instead:

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

Hence you can't get the image by the :medium style, but you can access its url and show it through an image tag, but styles won't work.

Upvotes: 1

Related Questions