【发布时间】:2015-03-08 14:52:36
【问题描述】:
当我在“show.html.erb”中插入图片标签(如下)时
<%= image_tag @lecture.thumbnail_url(:thumb) %>
缩略图确实显示在我的 localhost:3000 中。
但是,当我尝试在“index.html.erb”中插入相同的图像标签时,图像(缩略图)不会显示。相反,我收到一条错误消息,上面写着
讲座中的NoMethodError#index
显示 /Users/joebcvan/workspace/aca/app/views/lectures/index.html.erb 其中第 15 行提出:
nil:NilClass 的未定义方法 `thumbnail_url'
并且错误信息直接将错误指向图片标签行。
我尝试在 index.html.erb 中的任何位置插入图像标记(在我的 div 标记等之外)。但是,图像仍然没有显示 - 它只是在 index.html 中不起作用。厄伯。我尝试过使用 app/uploaders/thumbnail_uploader.rb,但它仍然无法正常工作。由于图像在 show.html.erb 中正确显示,我认为这不是问题..
我应该改用回形针...但是,我听说过 Carrierwave 的好消息,所以我真的很想让它发挥作用。
这是我的 index.html.erb
<div class="jumbotron">
<h2>Ipsum Lorem Ipsum Loream</h2>
<h3>Ipsum Lorem Ipsum Lorem <br>
Ipsum Lorem Ipsum Lorem<h3>
</div>
<div class="center">
<div class="row">
<% @lectures.each do |lecture| %>
<div class="col-md-3">
<div class="thumbnail">
<%= image_tag @lecture.thumbnail_url(:thumb) %>
<div class="caption">
<h3><%= lecture.name %></h3>
<p><%= number_to_currency(lecture.price) %></p>
<p><%= lecture.description %></p>
<p><%= "lecturer: #{lecture.user.name}" %></p>
<%= link_to 'Show', lecture, class: "btn btn-link" %>
</div>
</div>
</div>
<% end %>
</div>
</div>
<br>
<% if user_signed_in? %>
<%= link_to 'New Lecture', new_lecture_path %>
<% end %>
这是我的 show.html.er
<%= image_tag @lecture.thumbnail_url(:thumb) %>
<div class="row">
<div class="col-md-6">
<h2><strong>Lecture Title: </strong><%= @lecture.name %></h2>
<p><strong>Descriptions: </strong><%= @lecture.description %></p>
<p><strong>Price: </strong><%= @lecture.price %></p>
</div>
</div>
<% if current_user == @lecture.user %>
<%= link_to 'Edit', edit_lecture_path(@lecture) %> |
<% end %>
<%= link_to 'Back', lectures_path %>
这是我的应用程序/uploaders/thumbnail_uploader.rb
# encoding: utf-8
class ThumbnailUploader < 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 :file
# 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_fit => [200, 300]
#
# def scale(width, height)
# # do something
# end
# Create different versions of your uploaded files:
version :thumb do
process :resize_to_fit => [200, 200]
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
提前感谢您的出色帮助!
【问题讨论】:
标签: ruby-on-rails image carrierwave minimagick