devise cropping Images

17 views
Skip to first unread message

Furkan Adiguezel

unread,
May 26, 2014, 8:56:59 AM5/26/14
to plataforma...@googlegroups.com

I'm new at rails 4 and have some behavior, which I can't handle. I want to include a profile image in my Registrationscontroller, that checks, if an Avatar is included. I've got a Usermodel, which has_one Profile. And a Profile belongs_to an User. The Profile got the avatar attribute. How can I include this check in my rubycode.

Here my Applicationcontroller

  def configure_permitted_parameters
    ## Changed "profile" to "profile_attributes"
    devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:username, :email, :password, :password_confirmation, profile_attributes: [:anrede, :titel,  :vorname, :nachname, :user_id, :kanzlei, :kanlei_nr, :straße, :straße_nr, :plz, :stadt, :land, :telefon, :avatar, :avatar_cache]) }
    devise_parameter_sanitizer.for(:sign_in) { |u| u.permit(:login, :username, :email, :password, :remember_me) }
    devise_parameter_sanitizer.for(:account_update) { |u| u.permit(:username, :email, :password, :password_confirmation, :current_password) }
  end

And my Registrationcontroller

class Users::RegistrationsController < Devise::RegistrationsController
  before_filter :configure_permitted_parameters, if: :devise_controller?

  def update
    if params[:user].present?
      new_params = params.require(:user).permit(:email, :username, :current_password, :password, :password_confirmation)
      change_password = true
      if params[:user][:password].blank?
        params[:user].delete("password")
        params[:user].delete("password_confirmation")
        new_params = params.require(:user).permit(:email, :username)
        change_password = false
      end

      @user = User.find(current_user.id)
      is_valid = false

      if change_password
        is_valid = @user.update_with_password(new_params)
      else
        is_valid = @user.update_without_password(new_params)
      end

      if is_valid
        set_flash_message :notice, :updated
        sign_in @user, :bypass => true
        redirect_to after_update_path_for(@user)
      else
        render "edit"
      end
    end

    # GET /resource/sign_up
    def new
      build_resource({})
      respond_with self.resource
    end

    # POST /resource
    def create
      build_resource(sign_up_params)
      if params[:user][:profile][:avatar].present?
        render crop
      else
      resource_saved = resource.save
      yield resource if block_given?
      if resource_saved
        if resource.active_for_authentication?
          set_flash_message :notice, :signed_up if is_flashing_format?
          sign_up(resource_name, resource)
          respond_with resource, location: after_sign_up_path_for(resource)
        else
          set_flash_message :notice, :"signed_up_but_#{resource.inactive_message}" if is_flashing_format?
          expire_data_after_sign_in!
          respond_with resource, location: after_inactive_sign_up_path_for(resource)
        end

      else
        clean_up_passwords resource
        respond_with resource
      end
    end
  end
end

Please help, I would be really glad for any hints or helps.

Reply all
Reply to author
Forward
0 new messages