Reputation: 641
I am new in ruby on rails. I am trying to update the user avatar using devise and carrierwave. I have integrated carrierwave on another controller and it is working fine there,But I don't know why it is not working with devise.
I am using this tutorial
https://github.com/carrierwaveuploader/carrierwave/wiki/how-to:-use-carrierwave-with-devise
No error is showing on the form and logs are not showing any information either. I have checked the logs and in the update query there is no data about the avatar,while when I print the post data parameters they contain the multipart data.
This is the code
Logs
Started PUT "/users" for 10.0.2.2 at 2018-01-22 17:22:39 +0000
Cannot render console from 10.0.2.2! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
Processing by Users::RegistrationsController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"GbwVhRNMIJMnXIxF/v39fIWBSCOVN9/XEVXq9Tar+H0F0vq9JMDE8aM9efSqFCjr2WKaegsd2XhoLR5TyUyl8Q==", "avatar"=>#<ActionDispatch::Http::UploadedFile:0x007fc1a3288f40 @tempfile=#<Tempfile:/tmp/RackMultipart20180122-13485-1fn61vx.png>, @original_filename="Screen Shot 2018-01-18 at 8.28.55 PM.png", @content_type="image/png", @headers="Content-Disposition: form-data; name=\"avatar\"; filename=\"Screen Shot 2018-01-18 at 8.28.55 PM.png\"\r\nContent-Type: image/png\r\n">, "avatar_cache"=>"", "user"=>{"admin"=>"true", "email"=>"[email protected]", "first_name"=>"donation", "last_name"=>"simple", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "current_password"=>"[FILTERED]"}, "commit"=>"Update"}
[1m[36mUser Load (0.6ms)[0m [1m[34mSELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2[0m [["id", 450215437], ["LIMIT", 1]]
[1m[36mUser Load (0.5ms)[0m [1m[34mSELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2[0m [["id", 450215437], ["LIMIT", 1]]
[1m[35m (0.4ms)[0m [1m[35mBEGIN[0m
[1m[35mSQL (0.6ms)[0m [1m[33mUPDATE "users" SET "first_name" = $1, "last_name" = $2, "updated_at" = $3 WHERE "users"."id" = $4[0m [["first_name", "donation"], ["last_name", "simple"], ["updated_at", "2018-01-22 17:22:39.206951"], ["id", 450215437]]
[1m[35m (4.3ms)[0m [1m[35mCOMMIT[0m
Redirected to http://localhost:8090/admins
Completed 302 Found in 216ms (ActiveRecord: 6.4ms)
Started GET "/admins" for 10.0.2.2 at 2018-01-22 17:22:39 +0000
Cannot render console from 10.0.2.2! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
Processing by AdminsController#index as HTML
[1m[36mUser Load (0.6ms)[0m [1m[34mSELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2[0m [["id", 450215437], ["LIMIT", 1]]
[1m[36mUser Load (0.5ms)[0m [1m[34mSELECT "users"."admin_type" FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2[0m [["id", 450215437], ["LIMIT", 1]]
[1m[36mUser Load (0.4ms)[0m [1m[34mSELECT "users"."id", "users"."first_name", "users"."last_name", "users"."admin_type" FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2[0m [["id", 450215437], ["LIMIT", 1]]
[1m[35m (0.3ms)[0m [1m[34mSELECT SUM("donations"."amount_cents") FROM "donations"[0m
[1m[35m (0.6ms)[0m [1m[34mSELECT SUM("donations"."amount_cents") FROM "donations" WHERE "donations"."payment_type" = $1[0m [["payment_type", 2]]
[1m[35m (0.6ms)[0m [1m[34mSELECT SUM("donations"."amount_cents") FROM "donations" WHERE "donations"."payment_type" = $1[0m [["payment_type", 0]]
DEPRECATION WARNING: uniq is deprecated and will be removed from Rails 5.1 (use distinct instead) (called from index at /vagrant/donation-simple/app/controllers/admins_controller.rb:68)
[1m[35m (0.6ms)[0m [1m[34mSELECT DISTINCT "donations"."user_id" FROM "donations"[0m
[1m[36mCACHE (0.2ms)[0m [1m[34mSELECT "users"."admin_type" FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2[0m [["id", 450215437], ["LIMIT", 1]]
[1m[36mCACHE (0.1ms)[0m [1m[34mSELECT "users"."id", "users"."first_name", "users"."last_name", "users"."admin_type" FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2[0m [["id", 450215437], ["LIMIT", 1]]
[1m[36mCACHE (0.0ms)[0m [1m[34mSELECT SUM("donations"."amount_cents") FROM "donations"[0m
[1m[36mCACHE (0.0ms)[0m [1m[34mSELECT SUM("donations"."amount_cents") FROM "donations" WHERE "donations"."payment_type" = $1[0m [["payment_type", 2]]
[1m[36mCACHE (0.0ms)[0m [1m[34mSELECT SUM("donations"."amount_cents") FROM "donations" WHERE "donations"."payment_type" = $1[0m [["payment_type", 0]]
DEPRECATION WARNING: uniq is deprecated and will be removed from Rails 5.1 (use distinct instead) (called from index at /vagrant/donation-simple/app/controllers/admins_controller.rb:68)
[1m[36mCACHE (0.0ms)[0m [1m[34mSELECT DISTINCT "donations"."user_id" FROM "donations"[0m
Rendering admins/index.html.erb within layouts/admin
[1m[36mOrganization Load (0.6ms)[0m [1m[34mSELECT id, name, url FROM "organizations"[0m
Rendered admins/index.html.erb within layouts/admin (2.9ms)
Rendered layouts/_header.html.erb (13.6ms)
Rendered layouts/_sidebar.html.erb (0.6ms)
Rendered layouts/_footer.html.erb (0.6ms)
Completed 200 OK in 546ms (Views: 505.4ms | ActiveRecord: 4.6ms)
Started GET "/img/icon-color.png" for 10.0.2.2 at 2018-01-22 17:22:40 +0000
Cannot render console from 10.0.2.2! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
view file
<div class="row">
<div class="col-md-12">
<!-- BEGIN PROFILE SIDEBAR -->
<%= form_for(resource, as: resource_name, url: registration_path(resource_name), html: {:method => :put, :multipart => true}) do |f| %>
<% if resource.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(resource.errors.count, "error") %> prohibited this pet from being saved:</h2>
<ul>
<% resource.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="profile-sidebar">
<!-- PORTLET MAIN -->
<div class="portlet light profile-sidebar-portlet ">
<!-- SIDEBAR USERPIC -->
<div class="profile-userpic lg-img text-center">
<div class="fileinput fileinput-new" data-provides="fileinput">
<div class="fileinput-new thumbnail" style="width: 200px; height: 150px;">
<img src="http://www.placehold.it/200x150/EFEFEF/AAAAAA&text=no+image" alt=""> </div>
<div class="fileinput-preview fileinput-exists thumbnail" style="max-width: 200px; max-height: 150px; height: 150px;"> </div>
<div>
<span class="btn default btn-file">
<span class="fileinput-new"> Select image </span>
<span class="fileinput-exists"> Change </span>
<input type="file" name="avatar"> </span>
<input type="hidden" name="avatar_cache">
<a href="javascript:;" class="btn red fileinput-exists" data-dismiss="fileinput"> Remove </a>
</div>
</div>
</div>
<!-- END SIDEBAR USERPIC -->
<!-- SIDEBAR USER TITLE -->
<div class="profile-usertitle">
<div class="profile-usertitle-name"> </div>
<div class="profile-usertitle-job"> Developer </div>
</div>
<!-- END SIDEBAR USER TITLE -->
<!-- SIDEBAR BUTTONS -->
<div class="profile-userbuttons">
<button type="button" class="btn btn-circle green btn-sm">Follow</button>
<button type="button" class="btn btn-circle red btn-sm">Message</button>
</div>
<!-- END SIDEBAR BUTTONS -->
<!-- SIDEBAR MENU -->
<div class="profile-usermenu">
<ul class="nav">
<li>
<a href="page_user_profile_1.html">
<i class="icon-home"></i> Overview </a>
</li>
<li class="active">
<a href="page_user_profile_1_account.html">
<i class="icon-settings"></i> Account Settings </a>
</li>
<li>
<a href="page_user_profile_1_help.html">
<i class="icon-info"></i> Help </a>
</li>
</ul>
</div>
<!-- END MENU -->
</div>
<!-- END PORTLET MAIN -->
<!-- PORTLET MAIN -->
<div class="portlet light ">
<div>
<h4 class="profile-desc-title">About Marcus Doe</h4>
<span class="profile-desc-text"> Lorem ipsum dolor sit amet diam nonummy nibh dolore. </span>
<div class="margin-top-20 profile-desc-link">
<i class="fa fa-globe"></i>
<a href="http://www.keenthemes.com">www.keenthemes.com</a>
</div>
<div class="margin-top-20 profile-desc-link">
<i class="fa fa-twitter"></i>
<a href="http://www.twitter.com/keenthemes/">@keenthemes</a>
</div>
<div class="margin-top-20 profile-desc-link">
<i class="fa fa-facebook"></i>
<a href="http://www.facebook.com/keenthemes/">keenthemes</a>
</div>
</div>
</div>
<!-- END PORTLET MAIN -->
</div>
<!-- END BEGIN PROFILE SIDEBAR -->
<!-- BEGIN PROFILE CONTENT -->
<div class="profile-content">
<div class="row">
<div class="col-md-12">
<div class="portlet light ">
<div class="portlet-title tabbable-line">
<div class="caption caption-md">
<i class="icon-globe theme-font hide"></i>
<span class="caption-subject font-blue-madison bold uppercase">Profile Account</span>
</div>
</div>
<div class="portlet-body">
<div class="tab-content">
<!-- PERSONAL INFO TAB -->
<div class="tab-pane active" id="tab_1_1">
<%= devise_error_messages! %>
<%= f.hidden_field :admin, value: true %>
<div class="form-group">
<%= f.label :email,class:'control-label' %><br />
<%= f.email_field :email,class:'form-control',placeholder: 'Email', autofocus: true %>
</div>
<% if devise_mapping.confirmable? && resource.pending_reconfirmation? %>
<div>Currently waiting confirmation for: <%= resource.unconfirmed_email %></div>
<% end %>
<div class="form-group">
<%= f.label :first_name,class:'control-label' %><br />
<%= f.text_field :first_name,class:'form-control',placeholder: 'First name', required: false %>
</div>
<div class="form-group">
<%= f.label :last_name,class:'control-label' %><br />
<%= f.text_field :last_name,class:'form-control',placeholder: 'Last name', required: false %>
</div>
<div class="form-group">
<%= f.label :password,class:'control-label' %> <i>(leave blank if you don't want to change it)</i><br />
<%= f.password_field :password,class:'form-control',placeholder: 'Password', autocomplete: "off" %>
<% if @minimum_password_length %>
<br />
<em><%= @minimum_password_length %> characters minimum</em>
<% end %>
</div>
<div class="form-group">
<%= f.label :password_confirmation,class:'control-label' %><br />
<%= f.password_field :password_confirmation,class:'form-control',placeholder: 'Confirm Password', autocomplete: "off" %>
</div>
<div class="form-group">
<%= f.label :current_password,class:'control-label' %> <i>(we need your current password to confirm your changes)</i><br />
<%= f.password_field :current_password,class:'form-control',placeholder: 'Current Password',autocomplete: "off" %>
</div>
<div class="actions">
<%= f.submit "Update",:class => 'btn green' %>
</div>
</div>
<% end %>
<h3>Cancel my account</h3>
<p>Unhappy? <%= button_to "Cancel my account", registration_path(resource_name), data: { confirm: "Are you sure?" }, method: :delete %></p>
<%= link_to "Back", :back,:class => 'btn blue' %>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<!-- END PROFILE CONTENT -->
</div>
</div>
avatar_uploader
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}"
end
# Provide a default URL as a default if there hasn't been a file uploaded:
# def default_url(*args)
# # 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_whitelist
# %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
Application Controller
class ApplicationController < ActionController::Base
add_flash_types :success, :warning, :danger, :info
protect_from_forgery prepend: true
before_filter :configure_permitted_parameters, if: :devise_controller?
layout :layout_by_resource
helper_method :current_user
def logged_in_using_omniauth
session[:logged_in_using_omniauth].present?
end
helper_method :logged_in_using_omniauth
private
def layout_by_resource
if devise_controller?
"admin"
else
"application"
end
end
protected
def after_sign_in_path_for(resource)
if(current_user.admin)
'/admins'
else
'/donations/donor_history'
end
end
def after_sign_out_path_for(resource)
'/users/sign_in' #your path
end
def configure_permitted_parameters
devise_parameter_sanitizer.permit(:sign_up) do |user_params|
user_params.permit(:admin, :email, :password, :password_confirmation,:first_name,:last_name,:avatar, :avatar_cache, :remove_avatar)
end
devise_parameter_sanitizer.permit(:account_update) do |user_params|
user_params.permit(:admin, :email, :password, :password_confirmation,:current_password,:first_name,:last_name,:avatar, :avatar_cache, :remove_avatar)
end
end
end
I am trying from 3 days but still no success. Please help me in this problem.Thanks in advance.
Upvotes: 0
Views: 361
Reputation: 84
I think that you need to change your user model from this...
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :remember_me, :avatar, :avatar_cache, :remove_avatar
validates_presence_of :avatar
validates_integrity_of :avatar
validates_processing_of :avatar
end
to this...
class User < ActiveRecord::Base
mount_uploader :avatar, AvatarUploader
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :remember_me, :avatar, :avatar_cache, :remove_avatar
validates_presence_of :avatar
validates_integrity_of :avatar
validates_processing_of :avatar
end
Upvotes: 1
Reputation: 323
I'm trying to add a comment but can't? Anyway have you mounted the uploader to the user model?
Upvotes: 1