{"id":630,"date":"2022-08-30T15:04:33","date_gmt":"2022-08-30T15:04:33","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/09\/rails-devise-undefined-method-username-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:04:33","modified_gmt":"2022-08-30T15:04:33","slug":"rails-devise-undefined-method-username-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/rails-devise-undefined-method-username-collection-of-common-programming-errors\/","title":{"rendered":"Rails: Devise: Undefined Method `username&#39;-Collection of common programming errors"},"content":{"rendered":"<p>When using devise I keep getting this error when I try to load the sign up page with the new field &#8220;username&#8221;<\/p>\n<pre><code>undefined method `username' for #\n<\/code><\/pre>\n<p>This is in devise under registrations:<\/p>\n<pre><code>3:  resource_name, :url =&gt; registration_path(resource_name), html: { class: 'form-horizontal'}) do |f| %&gt;\n4:   \n5: \n6: \n7: \n8: \n9: \n<\/code><\/pre>\n<p>This is in models in user.rb<\/p>\n<pre><code>class User &lt; ActiveRecord::Base\n # Include default devise modules. Others available are:\n # :token_authenticatable, :confirmable,\n # :lockable, :timeoutable and :omniauthable\n   devise :database_authenticatable, :registerable,\n     :recoverable, :rememberable, :trackable, :validatable\n\n # Setup accessible (or protected) attributes for your model\n attr_accessible :username, :email, :password, :password_confirmation, :remember_me\n # attr_accessible :title, :body\n end\n<\/code><\/pre>\n<p>To set it up I put the following commands through terminal:<\/p>\n<pre><code>rails generate migration AddUsernameToUsers username:string\nbundle exec rake db:migrate\n<\/code><\/pre>\n<p>Through previous questions I put these commands through terminal:<\/p>\n<pre><code>rake db:schema:load\n<\/code><\/pre>\n<p>The error doesn&#8217;t let me even access the page. Unlike other questions where it happens after you click sign up.<\/p>\n<p><strong>Edit<\/strong><\/p>\n<p>After restarting my server a few times it is now automatically exiitng the local server with this error:<\/p>\n<pre><code>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'.\nExiting\n\/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)\n<\/code><\/pre>\n<p><strong>Edit<\/strong><\/p>\n<p>This is in models\/view.rb:<\/p>\n<pre><code>class View &lt; ActiveRecord::Base\n  # Include default devise modules. Others available are:\n  # :token_authenticatable, :confirmable,\n  # :lockable, :timeoutable and :omniauthable\n  devise :database_authenticatable, :registerable,\n     :recoverable, :rememberable, :trackable, :validatable\n\n  # Setup accessible (or protected) attributes for your model\n  attr_accessible :username, :email, :password, :password_confirmation, :remember_me,\n  # attr_accessible :title, :body\nend\n<\/code><\/pre>\n<p><strong>Edit<\/strong><\/p>\n<p>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.<\/p>\n<ol>\n<li>\n<p>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<\/p>\n<p>If you want to login only using your username, you&#8217;ll have to change your authentication_key from your devise.rb configuration file:<\/p>\n<pre><code>  # ==&gt; Configuration for any authentication mechanism\n  # Configure which keys are used when authenticating a user. The default is\n  # just :email. You can configure it to use [:username, :subdomain], so for\n  # authenticating a user, both parameters are required. Remember that those\n  # parameters are used only when authenticating and not when retrieving from\n  # session. If you need permissions, you should implement that in a before filter.\n  # You can also supply a hash where the value is a boolean determining whether\n  # or not authentication should be aborted when the value is not present.\n  config.authentication_keys = [ :username ]\n<\/code><\/pre>\n<p>Also you&#8217;ll have to modify your registration and session views according to your authentication_key.<\/p>\n<p>In devise\/registrations\/new.html.erb:<\/p>\n<pre><code>3:  resource_name, :url =&gt; registration_path(resource_name), html: { class: 'form-horizontal'}) do |f| %&gt;\n4: \n5:\n6: \n7: \n8: \n9:\n10: \n<\/code><\/pre>\n<p>In devise\/registrations\/edit.html.erb:<\/p>\n<pre><code>3:  resource_name, :url =&gt; registration_path(resource_name), html: { class: 'form-horizontal'}) do |f| %&gt;\n4: \n5:\n6: \n<\/code><\/pre>\n<p>In devise\/sessions\/new.html.erb:<\/p>\n<pre><code>3:  resource_name, :url =&gt; registration_path(resource_name), html: { class: 'form-horizontal'}) do |f| %&gt;\n4:\n5: \n6: \n7:\n8: \n<\/code><\/pre>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-09 21:09:06. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>When using devise I keep getting this error when I try to load the sign up page with the new field &#8220;username&#8221; undefined method `username&#8217; for # This is in devise under registrations: 3: resource_name, :url =&gt; registration_path(resource_name), html: { class: &#8216;form-horizontal&#8217;}) do |f| %&gt; 4: 5: 6: 7: 8: 9: This is in models [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-630","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/630","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=630"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/630\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=630"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=630"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=630"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}