Search code examples
ruby-on-railsimagecarrierwaveminimagick

Thumbnail image (used Carrierwave + minimagick) shows up in show.html.erb but not in index.html.er)


When I insert the image tag (below) in "show.html.erb"

<%= image_tag @lecture.thumbnail_url(:thumb) %>

the thumbnail does show up in my localhost:3000.

But, when I insert the same image tag in my "index.html.erb", which I am trying to do, the image (thumbnail) does not show up. Instead, I get an error message that says

NoMethodError in Lectures#index

Showing /Users/joebcvan/workspace/aca/app/views/lectures/index.html.erb where line #15 raised:

undefined method `thumbnail_url' for nil:NilClass

And the error message directly points the error to the image tag line.

I have tried inserting the image tag everywhere in the index.html.erb (out of my div tag etc..) But, the image still does not show up - It just doesn't work in the index.html.erb. I have tried playing around with the app/uploaders/thumbnail_uploader.rb, but it still does not work. Since the image shows up correctly in show.html.erb, I don't think this is a problem..

I should have used Paperclip instead... But, I have heard good things about Carrierwave, so I really want to get this to work.

Here is my 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 %>

And here's my 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 %>

Here is my 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

Thank you in advance for your awesome help!


Solution

  • In your index.html.erb It should be

    <%= image_tag lecture.thumbnail_url(:thumb) %>
    

    Because @lecture is not available in your index action, it's nil and thus the error, and you want to stay within scope @lectures.each do |lecture|

    In your controller

    You have:

    def show
      @lecture.find(params[:id])
    end
    

    This is available in your show.html.erb only

    In your index controller you have;

    def index
      @lectures = Lecture.all
    end
    

    @lectures is now available in index action only.

    You can define whatever you want in your controller actions, for example:

    def index
      @foo = 'bar'
    end
    

    Now @foo can called for index.html.erb as long as the variable is instance variable.

    Read more Here: http://guides.rubyonrails.org/action_controller_overview.html