Rails 4 - Carrier Wave Image Does Not Load

UPDATE . Thank you all for your help in trying to figure this out. I decided to abandon the attempt to learn and add another image attribute to the profile model. It needs to be done now.

I am trying to make an application in rails 4.

I have a custom model that has an avatar attribute on it.

I also have a profile model.

User: has one profile Profile: belongs to user

The reason I separate them is because the profile contains all the variables, and the user contains all the fixed attributes that cannot be changed without administrator permission.

The exception is that my user model has an avatar (image) attribute that I want to allow users to modify.

I have a custom carrier in other parts of my code and it works. However, in this case, something is wrong.

I have an avatar loader using:

class AvatarUploader < 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

  def cache_dir
    "#{Rails.root}/tmp/uploads"
  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

  process :resize_to_fit => [800, 800]
  # Create different versions of your uploaded files:
  version :thumb do
    process :resize_to_fill => [200,200]
  end

  version :profile do
    process :resize_to_fill => [345,245]
  end

  version :wide do
    process :resize_to_fill => [951,245]
  end

  version :preview do
    process :resize_to_fill => [90,90]
  end

  version :small do
    process :resize_to_fill => [35,35]
  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

In my user model, I have:

class User < ActiveRecord::Base
  mount_uploader :avatar, AvatarUploader
end

There is a whitelist in my user controller: avatar in strong settings.

In my profile, the strong parameters of the controller I tried to add:

params.require(:profile).permit(:title, :overview, user_attributes: [:avatar])

And I also tried to allow custom attributes in my profile model (as follows):

belongs_to :user
accepts_nested_attributes_for :user

I have a partial form in my users folder.

<%= simple_fields_for :user,  html: { multipart: true } do |f| %>
  <%= f.error_notification %>
  <div class="form-inputs">
    <%= f.input :avatar, as: :file, :label => "Add a profile image (head shot)" %>
  </div>
<% end %>

Partially included in my profile.

Then I want to display the user avatar in my profile view as:

<div class="col-md-5 col-lg-4 vc-photo" style= "background-image: url(<%= image_url @profile.user.avatar.url if @profile.user.avatar? %>);">&nbsp;</div>

However, it just displays a space.

I tried:

); ">  

but it is still just empty.

Can anyone see what I did wrong?

When I try to test the rails console by checking user.avatar, I get the following:

u.avatar  = > #, last_sign_in_ip: #, confirm_token: "73abb47df224dbc3a612b46ced66e1aba...", confirm_at: "2016-04-02 07:13:57", confirm_sent_at: "2016-04-02 22:59:44", unconfirmed_email: "testiest@gmail.com", failed_attempts: 0, unlock_token: nil, locked_at: nil, created_at: "2016-04-02 07:13:57", updated_at: "2016-04-02 22:59:43", : nil, : false > , @mounted_as =: avatar >

, :

  <%= render 'users/profileimgform', f: f %>

/profileimgform :

<%= simple_fields_for :user,  html: { multipart: true } do |ff| %>
  <%= f.error_notification %>

              <div class="form-inputs">


              <%= ff.input :avatar, as: :file, :label => "Add a profile image (head shot)" %>

              </div>


        <% end %>

, ff . "f" "ff", , , "f".

, , user.avatar "".

+4
4

, :

<%= simple_fields_for :user,  html: { multipart: true } do |f| %>
  <%= f.error_notification %>
  <div class="form-inputs">
    <%= f.input :avatar, as: :file, :label => "Add a profile image (head shot)" %>
  </div>
<% end %>

html, . , - :

<%= simple_form_for :profile do |f| %>
  ...
  <%= f.simple_fields_for :user do |ff| %>
    <%= ff.input :avatar %>
  <% end %>
<% end %>
+1

, form_for fields_for, :

<%= form_for @person do |f| %>
  Addresses:
  <ul>
    <%= f.fields_for :addresses do |addresses_form| %>
      <li>
        <%= addresses_form.label :kind %>
        <%= addresses_form.text_field :kind %>
      </li>
    <% end %>
  </ul>
<% end %>

-

  • form_for @person f
  • f f.fields_for :addresses addresses_form
  • addresses_form.input ...

, simple_fields_for simple_form, , form_for .


:

//user.rb

class User < ActiveRecord::Base
  mount_uploader :avatar, AvatarUploader
  has_one :profile
end

//profile.rb

class Profile < ActiveRecord::Base
  belongs_to :user
  accepts_nested_attributes_for :user
end

.

: http://guides.rubyonrails.org/form_helpers.html#building-complex-forms

//profile_controller.rb

class ProfileController < ApplicationController

  private

  def profile_params
    params.require(:profile).permit(:title, user_attributes: [:avatar])
  end
end

///edit.html.erb

<%= form_for :profile do |f| %>
  Avatar:
  <ul>
    <%= f.fields_for :user do |uf| %>
      <li>
        <%= uf.label :avatar %>
        <%= uf.file_field :avatar %>
      </li>
    <% end %>
  </ul>
<% end %>

, , , , .

0

I worked on the problem by moving the avatar from the user to the profile.

0
source

I had the same error. If you are on a Mac, try installing imagemagik again. I used homegrown. You just need to run this command:

brew install imagemagick
0
source

All Articles