How to manually create a new user and user session in Devise?

You can create a new Devise user simply by creating a new user model (see https://github.com/plataformatec/devise/wiki/How-To:-Manage-users-through-a-CRUD-interface) @user = User.new(:email => ‘test@example.com’, :password => ‘password’, :password_confirmation => ‘password’) @user.save To sign in your newly created user, use sign_in @user

How to specify devise_parameter_sanitizer for edit action?

Once again, it was a matter of reading the manual … The magic word is :account_update and thus the working version becomes def configure_permitted_parameters devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:email, :password, :password_confirmation, :firstname, :middlename, :lastname, :nickname) } devise_parameter_sanitizer.for(:account_update) { |u| u.permit(:email, :password, :password_confirmation, :current_password, :firstname, :middlename, :lastname, :nickname) } end Note that if you’re in the business … Read more

RoR Devise: Sign in with username OR email

I have found a solution for the problem. I’m not quite satisfied with it (I’d rather have a way to specify this in the initializer), but it works for now. In the user model I added the following method: def self.find_for_database_authentication(conditions={}) find_by(username: conditions[:email]) || find_by(email: conditions[:email]) end As @sguha and @Chetan have pointed out, another … Read more

Devise redirect after login fail

Create a custom_failure.rb in your lib directory, with: class CustomFailure < Devise::FailureApp def redirect_url your_path end def respond if http_auth? http_auth else redirect end end end In your Devise initializer, include: config.warden do |manager| manager.failure_app = CustomFailure end Make sure Rails is loading your lib files, in your application.rb : config.autoload_paths += %W(#{config.root}/lib) Don’t forget … Read more

Rails Devise: get object of the currently logged in user?

Devise creates convenience methods on the fly that represent your currently logged user. However you should note that the generated method name includes the class name of your user model. e.g. if your Devise model is called ‘User‘ then the currently logged in user can be accessed with ‘current_user‘, and if your Devise class is … Read more

ActionController::InvalidAuthenticityToken in RegistrationsController#create

Per the comments in the core application_controller.rb, set protect_from_forgery to the following: protect_from_forgery with: :null_session Alternatively, per the docs, simply declaring protect_from_forgery without a :with argument will utilize :null_session by default: protect_from_forgery # Same as above UPDATE: This seems to be a documented bug in the behavior of Devise. The author of Devise suggests disabling … Read more

“WARNING: Can’t mass-assign protected attributes”

Don’t confuse attr_accessor with attr_accessible. Accessor is built into Ruby and defines a getter method – model_instance.foo # returns something – and a setter method – model_instance.foo = ‘bar’. Accessible is defined by Rails and makes the attribute mass-assignable (does the opposite of attr_protected). If first_name is a field in your model’s database table, then … Read more