Rails: Devise: Undefined Method `username'-Collection of common programming errors
When using devise I keep getting this error when I try to load the sign up page with the new field “username”
undefined method `username' for #
This is in devise under registrations:
3: resource_name, :url => registration_path(resource_name), html: { class: 'form-horizontal'}) do |f| %>
4:
5:
6:
7:
8:
9:
This is in models in user.rb
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable,
# :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
# Setup accessible (or protected) attributes for your model
attr_accessible :username, :email, :password, :password_confirmation, :remember_me
# attr_accessible :title, :body
end
To set it up I put the following commands through terminal:
rails generate migration AddUsernameToUsers username:string
bundle exec rake db:migrate
Through previous questions I put these commands through terminal:
rake db:schema:load
The error doesn’t let me even access the page. Unlike other questions where it happens after you click sign up.
Edit
After restarting my server a few times it is now automatically exiitng the local server with this error:
Called from: /usr/local/rvm/gems/ruby-1.9.3-p194/gems/actionpack- 3.2.8/lib/action_dispatch/middleware/session/abstract_store.rb:28:in `initialize'.
Exiting
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:469:in `load': /Users/hunter/first_app/app/models/view.rb:11: syntax error, unexpected keyword_end (SyntaxError)
Edit
This is in models/view.rb:
class View < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable,
# :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
# Setup accessible (or protected) attributes for your model
attr_accessible :username, :email, :password, :password_confirmation, :remember_me,
# attr_accessible :title, :body
end
Edit
I removed the comma at the end of :remember_me in models/view.rb and now the server works. I can now load it on localhost:3000. However, when I click the sign up page I get the same error as before.
-
If you want login using either your username or password, you have a very good explanation over here: Devise login using your username or email address
If you want to login only using your username, you’ll have to change your authentication_key from your devise.rb configuration file:
# ==> Configuration for any authentication mechanism # Configure which keys are used when authenticating a user. The default is # just :email. You can configure it to use [:username, :subdomain], so for # authenticating a user, both parameters are required. Remember that those # parameters are used only when authenticating and not when retrieving from # session. If you need permissions, you should implement that in a before filter. # You can also supply a hash where the value is a boolean determining whether # or not authentication should be aborted when the value is not present. config.authentication_keys = [ :username ]
Also you’ll have to modify your registration and session views according to your authentication_key.
In devise/registrations/new.html.erb:
3: resource_name, :url => registration_path(resource_name), html: { class: 'form-horizontal'}) do |f| %> 4: 5: 6: 7: 8: 9: 10:
In devise/registrations/edit.html.erb:
3: resource_name, :url => registration_path(resource_name), html: { class: 'form-horizontal'}) do |f| %> 4: 5: 6:
In devise/sessions/new.html.erb:
3: resource_name, :url => registration_path(resource_name), html: { class: 'form-horizontal'}) do |f| %> 4: 5: 6: 7: 8:
Originally posted 2013-11-09 21:09:06.