ruby-on-rails - Rails 中 image_tag 和回形针的 URL 错误

标签 ruby-on-rails paperclip

我在 Rails 应用程序中使用 Paperclip gem 来上传图像。但在 View 中,没有图像。缺失图像的路径为http://localhost:3000/assets/ 。 这是代码:

<div class="advert_images">
  Фото: <br>
  <% if @advertising.image.url %>
    <%= image_tag(@advertising.image) %>    
  <% end %>
  <br>
  Розташування на карті: <br>
  <% if @advertising.map.url(:normal) %>
    <%= image_tag(@advertising.map.url(:normal)) %>
  <% end %>
</div>

奇怪的是当输入代码<%= @advertising.image.url %>时这向我显示了现有图像的正确路径(例如 /system/adverts/images/000/001/408/original/luxot.gif?1355951576 )。

型号:

# -*- encoding : utf-8 -*-
class Advert < ActiveRecord::Base
attr_accessible :adress, :category, :city, :desc, :map_path, :name, :image_path, :map,        :image
has_attached_file :map, :styles => {:normal => "350x265", :thumb => "250x165"}
has_attached_file :image, :styles => {:normal => "350x265", :thumb => "250x165"}

  validates :name,  :length => {:in => 2..20}, :presence => true
  validates :category,                         :presence => true
  validates :city,                             :presence => true
  validates :adress,                           :presence => true


scope :by_city, lambda {|city| where("city = ?", city)}
scope :by_category, lambda {|category| where("category = ?", category)}

end

最佳答案

找到了解决方案。 我更改了回形针存储图像的默认路径。

  has_attached_file :image,
                :url => "/assets/:id/:style/:basename.:extension",
                :path =>  
                ":rails_root/app/assets/images/:id/:style/:basename.:extension"

关于ruby-on-rails - Rails 中 image_tag 和回形针的 URL 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13961915/

相关文章:

ruby-on-rails - 使用来自请求 header 的 auth_token 而不是来自 Rails 3/devise 的 POST/PUT 参数

ruby-on-rails - ruby 中的 "Related Items"

mysql - 在 activerecord 中可能使用连接将两个 SQL 查询减少为一个

ruby-on-rails - RSpec + 回形针 : identify: no decode delegate for this image format

ruby-on-rails - rails 4 回形针上传到亚马逊 s3 不起作用

ruby-on-rails - JPEG 无法被Identify、Paperclip、ImageMagick on Rails 3.1.3 OSX Mountain Lion 识别

ruby-on-rails - Net-ssh 执行超时?

mysql - 我正在尝试在 Apache 上运行 Rails,但无法进入开发模式

ruby-on-rails - 带有嵌套模型的 rails 和回形针 : uploads from form not working

ruby-on-rails - ImageMagick 和 Paperclip 找不到我的文件在 S3 (Jcrop) 上的位置