Not Displaying Image with Carrierwave using Rails 6

20 Views Asked by At

I was looking for a way to upload photos to the database and display images on a page. I am using Carrierwave. The image uploads correctly, but when it goes to display, it will not. When I tried to access the image using this path http://localhost:3000/packs/profile/image/11/5934de58-d4c6-4016-a395-773a785390a9.png the image could not be accessed. But when I tried to use this path http://localhost:3000//packs/profile/image/11/5934de58-d4c6-4016-a395-773a785390a9.png I could access the image.

Here is my model

class Profile < ApplicationRecord
  mount_uploader :image, ImageUploader
end

Here's the form I used to upload the image

<%= form_for profile, url: url, method: method,
             html: { multipart: true, class: 'form' } do |f| %>
  <div class="error-msg">
    <% if profile.errors.any? %>
      <ul>
        <% profile.errors.full_messages.each do |message| %>
          <li><%= message %></li>
        <% end %>
      </ul>
    <% end %>
  </div>

  <div class="form-group">
    <%= f.label :image, Profile.human_attribute_name(:image) %>
    <%= f.file_field :image, class: 'form-control' %>
  </div>

  <div class="form-group">
    <%= f.label :username, Profile.human_attribute_name(:username) %>
    <%= f.text_field :username, required: true, class: 'form-control' %>
  </div>

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

This is how I call the image to display in my page

<%= image_tag profile.image.url if profile.image.url.present? %>
0

There are 0 best solutions below