Note that there are some explanatory texts on larger screens.

plurals
  1. POLogging in with username using Devise with Rails 4
    text
    copied!<p>I'm using Rails 4.0 and Devise 3.0.0.rc.</p> <p>I installed Devise with its defaults - i.e. sign up/in using your email address. However, I'd like the users to be able to sign up with a username instead of their email address. I've followed the railscasts in terms of which parameters to change, plus any advice I can find on here for things I might need to change for Rails 4. However, whenever I try to create a new user, I am getting the error 'email can't be blank'. Does anyone have an idea as to what is causing this, and how I can fix it? Thanks!!</p> <p>Code:</p> <p>application controller:</p> <pre><code>class ApplicationController &lt; ActionController::Base # Prevent CSRF attacks by raising an exception. # For APIs, you may want to use :null_session instead. protect_from_forgery with: :exception before_filter :configure_permitted_parameters, if: :devise_controller? protected def configure_permitted_parameters devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:username, :password, :password_confirmation) } devise_parameter_sanitizer.for(:sign_in) { |u| u.permit(:username, :password, :remember_me) } end end </code></pre> <p>Sign in (sign up form is pretty much the same):</p> <pre><code>&lt;%= form_for(resource, :as =&gt; resource_name, :url =&gt; session_path(resource_name)) do |f| %&gt; &lt;div&gt;&lt;%= f.label :username %&gt;&lt;br /&gt; &lt;%= f.text_field :username, :autofocus =&gt; true %&gt;&lt;/div&gt; </code></pre> <p>In devise.rb config:</p> <pre><code>config.authentication_keys = [ :username ] </code></pre> <p>users controller:</p> <pre><code>private # Use callbacks to share common setup or constraints between actions. def set_user @user = User.find(params[:id]) end # Never trust parameters from the scary internet, only allow the white list through. def user_params params.require(:user).permit(:first_name, :middle_names, :last_name, :date_of_birth, :gender, :line_1, :line_2, :line_3, :town, :county, :postcode, :contact_number, :email, :username, :password, :password_confirmation, :remember_me) end </code></pre> <p>user.rb:</p> <pre><code>protected def self.find_for_database_authentication(warden_conditions) conditions = warden_conditions.dup login = conditions.delete(:login) where(conditions).where(["lower(username) = :value", { :value =&gt; login.downcase }]).first end </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload