我正在使用Rails 4并尝试在我的Posts#Show视图中运行@ post.image.thumb.url但是我收到此错误:
帖子中的NoMethodError#show 显示/home/vagrant/code/nonofficejobs/app/views/posts/show.html.erb第11行提出:
未定义的方法`thumb'for# 提取的来源(第11行): 8 9 10 11 12 13 14
<%= link_to @post.user.name, @post.user %>
</small>
<p><%= markdown_to_html @post.body %></p>
<div><%= image_tag(@post.image.thumb.url) if @post.image? %></div>
</div>
这是我的image_uploader.rb:
# encoding: utf-8
class ImageUploader < CarrierWave::Uploader::Base
# Include RMagick or MiniMagick support:
# include CarrierWave::RMagick
include CarrierWave::MiniMagick
# Choose what kind of storage to use for this uploader:
storage :fog
# Override the directory where uploaded files will be stored.
# This is a sensible default for uploaders that are meant to be mounted:
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
# Provide a default URL as a default if there hasn't been a file uploaded:
# def default_url
# # For Rails 3.1+ asset pipeline compatibility:
# # ActionController::Base.helpers.asset_path("fallback/" + [version_name, "default.png"].compact.join('_'))
#
# "/images/fallback/" + [version_name, "default.png"].compact.join('_')
# end
# Process files as they are uploaded:
process resize_to_fill: [90, 90]
#
# def scale(width, height)
# # do something
# end
# Create different versions of your uploaded files:
# version :tiny do
# process resize_to_fill: [20, 20]
# end
version :small do
process resize_to_fill: [30, 30]
end
# version :profile do
# process resize_to_fill: [45, 45]
# end
process resize_to_fill: [300, 300]
version :thumb do
process resize_to_fill: [50, 50]
end
# Add a white list of extensions which are allowed to be uploaded.
# For images you might use something like this:
def extension_white_list
%w(jpg jpeg gif png)
end
# Override the filename of the uploaded files:
# Avoid using model.id or version_name here, see uploader/store.rb for details.
# def filename
# "something.jpg" if original_filename
# end
end
答案 0 :(得分:0)
尝试从
更改您的代码<%= image_tag(@post.image.thumb.url) if @post.image? %>
到
<%= image_tag((@post.image.url(:thumb)) %>