Rails paperclip not showing image

故事扮演 提交于 2020-01-05 08:54:10

问题


I am new to paperclip, and I wanted to see how it would work. I generated a simple model Monkey and got the following:

rails g scaffold monkey description age:datetime
rails g paperclip monkey facepic
rake db:migrate

Model

class Monkey< ActiveRecord::Base

    has_attached_file :facepic, :styles => { :medium => "300x300>", :thumb => "100x100>" }

end

View new/edit

<%= form_for @monkey, :url => monkies_path, :html => { :multipart => true } do |f| %>
 ...
  <div class="field">
    <%= f.label :facepic %><br>
    <%= f.file_field :facepic %>
  </div>

View show

 <%= image_tag @monkey.facepic.url %>

Controller

@monkey = Monkey.new(monkey_params)

I can create new monkeys, but the show view doesn't seem to find the uploaded file. I have no error messages, except a routing error to 'missing.png'. There is no trace to the uploaded image. I am using Rails 4.1.6. What am I missing here? How do I troubleshoot this thing? The gem is installed and also imagemagick is installed.

This is what the logs say:

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

Started GET "/monkies/new" for 127.0.0.1 at 2014-09-19 14:40:22 +0200
Processing by MonkiesController#new as HTML
  Rendered monkies/_form.html.erb (4.0ms)
  Rendered monkies/new.html.erb within layouts/application (5.0ms)
Completed 500 Internal Server Error in 12ms

ActionView::Template::Error (No route matches {:action=>"show", :controller=>"monkies"} missing required keys: [:id]):

There is no error message displayed however when I create a new monkey... :'(

EDIT:

The Monkey model is created, but the paperclip columns remain empty.


回答1:


This error clearly show that your images are not getting saved because path and url not specified in your has_attached_file block. It should be like :

has_attached_file :facepic, 
  :path => ":rails_root/public/system/:attachment/:id/:style/:filename",
  :url => "/system/:attachment/:id/:style/:filename", 
  :styles => { :medium => "300x300>", :thumb => "100x100>" },
  :default_url => "path to default image"

here default_url show image that you want if no image uploaded. For more detail you can go here http://rdoc.info/gems/paperclip/4.2.0/Paperclip/ClassMethods%3ahas_attached_file .

And for other error you can follow this link Paperclip::Errors::MissingRequiredValidatorError with Rails 4




回答2:


Starting with Paperclip version 4.0, all attachments are required to include a content_type validation, a file_name validation, or to explicitly state that they're not going to have either.

Paperclip raises Paperclip::Errors::MissingRequiredValidatorError error if you do not do any of this.

In your case, you can add any of the following line to your Post model, after specifying has_attached_file :image

Option 1: Validate content type

validates_attachment_content_type :image, :content_type => ["image/jpg", "image/jpeg", "image/png", "image/gif"]

-OR- another way

validates_attachment :image, content_type: { content_type: ["image/jpg", "image/jpeg", "image/png", "image/gif"] }

-OR- yet another way

is to use regex for validating content type.

Option 2: Validate filename

validates_attachment_file_name :avatar, :matches => [/png\Z/, /jpe?g\Z/, /gif\Z/]

Option 3: Do not validate

If for some crazy reason (can be valid but I cannot think of one right now), you do not wish to add any content_type validation and allow people to spoof Content-Types and receive data you weren't expecting onto your server then add the following:

do_not_validate_attachment_file_type :image

Note:

Specify the MIME types as per your requirement within content_type/ matches options above. I have just given a few image MIME types for you to start with.

Reference:

Refer to Paperclip: Security Validations, if you still need to verify. :)

For details you can go to This question




回答3:


I am sorry to say that my problem eventually was a bunch of things:

  1. Had to add :facepic to the strong params
  2. Had to add validations of content type to the model
  3. Paperclip has a dependency... the file-command, which isn't shipped on Windows. which can be downloaded here: http://sourceforge.net/projects/gnuwin32/?source=typ_redirect

After all three, it works like a charm!

Thank you all!



来源:https://stackoverflow.com/questions/25934465/rails-paperclip-not-showing-image

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!