Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

devise registrations controller + paperclip

i am trying to override devise registrations controller so that user would be able to upload his avatar along with changing other data, and crop the userpic after upload.

i added all necesarry user paperclip attributes, created crop view, and my registrations controller looks like that:

class RegistrationsController < Devise::RegistrationsController
    def update

    if params[resource_name][:avatar].blank?
            super
    else
            @user=resource
       respond_to do |format|
         if resource.update_attributes(params[resource_name])
            flash[:notice]='Avatar successfully uploaded.'
            format.html {
                    render :action => 'crop'
            }
           format.xml  { head :ok }
          else
            format.html { render :action => "editpicture" }
            format.xml  { render :xml => @demotivator.errors, :status => :unprocessable_entity }
          end
       end
    end
    end

end

but when i submit the form with picture, nothing happens, except that firefox shows "loading..." forever! absolutely no updates in development log.. :(

could anyone tell me what could i be doing wrong?

ps. user edit form looks like that:

<%= form_for(@user, :url => registration_path(@user), :html => {:id => "userpic_form", :method => :put, :multipart => true}) do |f| %>
 <p class="box1_po">Current password: <%= f.password_field :current_password %></p>
 <p class="box1_po">Please select your user picture:
                                            <%= f.file_field :avatar  %>
 </p>
 <input type="submit" class="usubmit"><%= link_to "UPLOAD", "#", :onclick => "$('#userpic_form').submit();"%>
<% end %>
like image 480
Pavel K. Avatar asked Apr 21 '11 00:04

Pavel K.


2 Answers

occurs i just had to add

attr_accessible :avatar

in User model, and it started working properly

like image 53
Pavel K. Avatar answered Nov 10 '22 16:11

Pavel K.


If you're using Rails 4, add the following to the RegistrationsController

# get devise to recognize the custom fields of the user model
before_filter :configure_permitted_parameters, if: :devise_controller?

protected

    def configure_permitted_parameters
        devise_parameter_sanitizer.for(:account_update) do |u|
            u.permit(:avatar, :email, :password, :password_confirmation)
        end
    end
like image 2
Hopstream Avatar answered Nov 10 '22 18:11

Hopstream