缩略图图像(使用Carrierwave + minimagick)显示在show.html.erb中,但不显示在index.html.er中)

时间:2015-03-08 14:52:37

标签: ruby-on-rails image carrierwave minimagick

当我在“show.html.erb”

中插入图片标签(下方)时
<%= image_tag @lecture.thumbnail_url(:thumb) %>

缩略图确实显示在我的localhost:3000。

但是,当我在我的“index.html.erb”中插入相同的图像标记时,我试图这样做,图像(缩略图)不显示。相反,我收到一条错误消息,上面写着

讲座#index

中的NoMethodError

显示/Users/joebcvan/workspace/aca/app/views/lectures/index.html.erb第15行:

未定义方法`thumbnail_url'代表nil:NilClass

错误消息直接将错误指向图像标记行。

我已经尝试在index.html.erb(我的div标签等)中的任何地方插入图像标记。但是,图像仍然没有显示 - 它只是在index.html中不起作用。 ERB。我试过玩app / uploaders / thumbnail_uploader.rb,但它仍然不起作用。由于图像在show.html.erb中正确显示,我认为这不是问题..

我本应该使用Paperclip ......但是,我听说过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 %>

这是我的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

提前感谢您的大力帮助!

1 个答案:

答案 0 :(得分:0)

在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