缩略图(使用 Carrierwave + minimagick)显示在 show.html.erb 但不在 index.html.er 中)
Thumbnail image (used Carrierwave + minimagick) shows up in show.html.erb but not in index.html.er)
当我在 "show.html.erb"
中插入图片标签(下图)时
<%= image_tag @lecture.thumbnail_url(:thumb) %>
缩略图确实出现在我的 localhost:3000 中。
但是,当我尝试在 "index.html.erb" 中插入相同的图像标签时,图像(缩略图)没有显示。相反,我收到一条错误消息,内容为
讲座中没有方法错误#index
显示 /Users/joebcvan/workspace/aca/app/views/lectures/index.html.erb 第 15 行出现的位置:
nil:NilClass
的未定义方法“thumbnail_url”
而且报错信息直接将错误指向图片标签行
我已经尝试在索引中的任何地方插入图像标签。html.erb(在我的 div 标签等之外。)但是,图像仍然没有显示 - 它只是没有'无法在索引中工作。html.erb。我试过 app/uploaders/thumbnail_uploader.rb,但它仍然不起作用。由于图像在 show.html.erb 中正确显示,我认为这不是问题..
我应该改用回形针...但是,我听说过有关 Carrierwave 的好东西,所以我真的很想让它发挥作用。
这是我的索引。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 %>
这是我的 app/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
提前感谢您的大力帮助!
在你的index.html.erb中应该是
<%= image_tag lecture.thumbnail_url(:thumb) %>
因为 @lecture 在您的索引操作中不可用,所以它是 nil,因此是错误,您希望保持在范围内 @lectures.each do |lecture|
在你的控制器中
您有:
def show
@lecture.find(params[:id])
end
这仅适用于您的 show.html.erb
在您的索引控制器中;
def index
@lectures = Lecture.all
end
@lectures 现在仅在索引操作中可用。
您可以在控制器操作中定义您想要的任何内容,例如:
def index
@foo = 'bar'
end
现在只要变量是实例变量,@foo 就可以调用 index.html.erb。
在此处阅读更多内容:
http://guides.rubyonrails.org/action_controller_overview.html
当我在 "show.html.erb"
中插入图片标签(下图)时<%= image_tag @lecture.thumbnail_url(:thumb) %>
缩略图确实出现在我的 localhost:3000 中。
但是,当我尝试在 "index.html.erb" 中插入相同的图像标签时,图像(缩略图)没有显示。相反,我收到一条错误消息,内容为
讲座中没有方法错误#index
显示 /Users/joebcvan/workspace/aca/app/views/lectures/index.html.erb 第 15 行出现的位置:
nil:NilClass
的未定义方法“thumbnail_url”而且报错信息直接将错误指向图片标签行
我已经尝试在索引中的任何地方插入图像标签。html.erb(在我的 div 标签等之外。)但是,图像仍然没有显示 - 它只是没有'无法在索引中工作。html.erb。我试过 app/uploaders/thumbnail_uploader.rb,但它仍然不起作用。由于图像在 show.html.erb 中正确显示,我认为这不是问题..
我应该改用回形针...但是,我听说过有关 Carrierwave 的好东西,所以我真的很想让它发挥作用。
这是我的索引。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 %>
这是我的 app/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
提前感谢您的大力帮助!
在你的index.html.erb中应该是
<%= image_tag lecture.thumbnail_url(:thumb) %>
因为 @lecture 在您的索引操作中不可用,所以它是 nil,因此是错误,您希望保持在范围内 @lectures.each do |lecture|
在你的控制器中
您有:
def show
@lecture.find(params[:id])
end
这仅适用于您的 show.html.erb
在您的索引控制器中;
def index
@lectures = Lecture.all
end
@lectures 现在仅在索引操作中可用。
您可以在控制器操作中定义您想要的任何内容,例如:
def index
@foo = 'bar'
end
现在只要变量是实例变量,@foo 就可以调用 index.html.erb。
在此处阅读更多内容: http://guides.rubyonrails.org/action_controller_overview.html