devise registrations controller + paperclip
Asked Answered
K

3

7

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 %>
Kobold answered 21/4, 2011 at 0:27 Comment(0)
K
7

occurs i just had to add

attr_accessible :avatar

in User model, and it started working properly

Kobold answered 8/5, 2011 at 11:8 Comment(0)
H
2

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
Hearsh answered 28/12, 2013 at 4:23 Comment(0)
G
0
  1. Make sure the param is permitted in the controller like below: `

    def configure_permitted_parameters
          devise_parameter_sanitizer.permit(:account_update, keys: [:firstname,
                      :lastname, :username, :password, :email, :bio, 
                      :avatar,:password_confirmation, :current_password ])
    end`
    
  2. Make sure you add this tag: :html => { :multipart => true } to your form:

    <%= simple_form_for(resource, as: resource_name, url: registration_path(resource_name), html: { method: :put }, :html => { :multipart => true }) do |f| %>

Geophyte answered 25/3, 2017 at 17:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.