Paperclip images not Displaying in rails app

本小妞迷上赌 提交于 2019-12-11 04:34:58

问题


I'm using paperclip to upload images to the site but when I do, it shows the image placeholder with a question mark, not the image itself. What Am I doing wrong? In the model:

     attr_accessor :photo_file_name
     attr_accessor :photo_content_type
    attr_accessor :photo_file_size
    attr_accessor :photo_updated_at
    attr_accessible :photo


#paperclip-------------------------------
 has_attached_file :photo,
                 :url => "/:attachment/:id/:style/:basename.:extension",
                 :path => ":rails_root/public/:attachment/:id/:style/:basename.:extension"

In the view:

<p id="notice"><%= notice %></p>
<div id="container">
<div id="content">
<%= image_tag @post.photo.url %>
<p>
  <b>Title:</b>
   <%= @post.title %>

and in the form I have:

<div class="field">
<%= post_form.file_field :photo %>
</div>

回答1:


Did you check whether the image file is stored :rails_root/public/:attachment/:id/:style/ yet?

Right click to the image place holder to check the url of the image, is it right?




回答2:


Get rid of all the accessor stuff and make sure your form returns something like:

params[:post][:photo]

Typically via:

form_for @post do |f|
  f.file_field :photo
end

Then, in your posts controller, you can update or otherwise save a particular post with a :photo attached. Display the photo by simply tagging it image_tag @post.photo, Paperclip can do the rest.

I highly recommend removing your :url and :path options and just use the default for now, which is fine for most applications. It includes the system/ directory that is symlinked as shared by default under Capistrano, which is nice.



来源:https://stackoverflow.com/questions/9170335/paperclip-images-not-displaying-in-rails-app

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