开发者

Rails 3 & paperclip - not rendering images

开发者 https://www.devze.com 2023-02-19 16:04 出处:网络
I\'ve just started using Paperclip today and am having issues getting images to render.After some wrangling the photos are 开发者_如何学编程saving in the correct directory but there is a routing/rende

I've just started using Paperclip today and am having issues getting images to render. After some wrangling the photos are 开发者_如何学编程saving in the correct directory but there is a routing/rendering error:

ActionController::RoutingError (No route matches "/public/system/products/19/original/puppies-3.jpg")

However, the images are definitely saving in the correct directory. This is what's in my product model:

class Product < ActiveRecord::Base

validates :title, :presence => true
validates :description, :presence => true
validates :category, :presence => true
validates_attachment_presence :photo
validates_attachment_size :photo, :less_than => 5.megabytes

attr_accessible :photo, :photo_file_name, :photo_content_type, :photo_file_size,    :photo_updated_at
attr_accessible :title, :description, :category, :price

has_attached_file :photo, :styles => { :small => "150x150>", :large => "400x400>" },
                :path => ":rails_root/public/system/products/:id/:style/:basename.:extension", 
                :url  => "/system/products/:id/:style/:basename.:extension"
end

This is in my view:

<%= image_tag @product.photo.url %>

At the moment it's simply returning the image basename instead of the image itself, any thoughts? Products is available as a resource in routes.rb, but do I need to explicitly make photos available also somehow? I'm also fairly new to Rails so struggling a little bit...


Are you sure that product with that id has a image attached? I think some of your products dont have images attached but u still looking for them. Maybe try to set a default_url. Something like this:

has_attached_file :photo, :default_url => "/images/missing.png"

This will show that image set in default_url for the products what dont have images attached.


It's ok, I've fixed it. I had left a field in my form which was naming the photo_file_name from when I couldn't get the files to save and was trying to manually override it to get it to work (face-palm). It's amazing how much clarity comes from a few hours sleep.

0

精彩评论

暂无评论...
验证码 换一张
取 消