繁体   English   中英

缩略图图像(用于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”中插入相同的图像标签时,图像(缩略图)未显示。 相反,我收到一条错误消息,内容为

Lectures#index中的NoMethodError

显示/Users/joebcvan/workspace/aca/app/views/lectures/index.html.erb在第15行出现的地方:

nil:NilClass的未定义方法`thumbnail_url'

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

我尝试将image标签插入到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 %>

这是我的应用程序/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|范围内@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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM