Rohan Pillai
Rohan Pillai

Reputation: 967

Referred CarrierWave's official documentation but image not displaying

I am developing a project in Ruby on Rails and I have to upload an image for it. I came across CarrierWave for that, and hence I referred its official documentation for it. However, when I upload an image, the image is getting stored in the database but when I try to display it with <%= image_tag @books.cover.url %> I get the following error:

undefined method `url' for "#ActionDispatch::Http::UploadedFile:0x000000000e983c68>":String

Please find the code for the files below:
app/views/books/_form.html.erb

<%= form_with(model: book, local: true) do |form| %>
  <% if book.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(book.errors.count, "error") %> prohibited this book from being saved:</h2>

      <ul>
        <% book.errors.full_messages.each do |message| %>
          <li><%= message %></li>
        <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= form.label :cover %>
    <%= form.file_field :cover %>
  </div>

  <div class="actions">
    <%= form.submit %>
  </div>
<% end %>


app/models/book.rb

class Book < ApplicationRecord
    belongs_to :library

    mount_uploader :image, ImageUploader
end


app/views/books/show.html.erb

<p>
    <strong>Cover:</strong>
    <%= image_tag @book.cover.url if @book.cover? %>
</p>


app/uploaders/image_uploader.rb

# encoding: utf-8

class ImageUploader < 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 :scale => [200, 300]
  #
  # def scale(width, height)
  #   # do something
  # end

  # Create different versions of your uploaded files:
  version :thumb do
    process :resize_to_fit => [50, 50]
  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(jpeg 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


Gemfile

# Upload Images
gem 'carrierwave'
gem 'mini_magick'

Ruby Version: 2.6.4
Rails Version: 6.0.0

I read a few questions on StackOverflow as well for example this, but no solution worked for me. I even tried reading a few online tutorials for CarrierWave but they use the same code as I do and yet the code works for them but not for me. Please let me know if anyone has encountered this before or if you require any more information.

Upvotes: 0

Views: 38

Answers (1)

Shaun Decker
Shaun Decker

Reputation: 46

I think you want to mount the uploader for :cover.

class Book < ApplicationRecord
    belongs_to :library
    mount_uploader :cover, ImageUploader
end

Upvotes: 1

Related Questions