【问题标题】:Thumbnail image (used Carrierwave + minimagick) shows up in show.html.erb but not in index.html.er)缩略图(使用 Carrierwave + minimagick)显示在 show.html.erb 但不在 index.html.er 中)
【发布时间】: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


    【解决方案1】:

    在你的 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 中?但不在 index.html.erb 中?? @lecture 在 show.html.erb 中可用吗??
    • 在索引更改 到 删除实例 '@' 并访问讲座参数与您对 number_to_currency(lecture.price) 所做的相同 - 只是在您的索引 html erb
    • 非常感谢大家!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-07-21
    • 1970-01-01
    • 2020-03-24
    • 1970-01-01
    • 2021-04-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多