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 CarrierWave::MiniMagick
storage :fog
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
def cache_dir
"#{Rails.root}/tmp/uploads"
end
process :resize_to_fit => [800, 800]
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
def extension_white_list
%w(jpg jpeg gif png)
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? %>);"> </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 "".