Rails: Devise: Undefined Method `username '

When using the utility, I continue to get this error when I try to load the registration page with a new "username" field

undefined method `username' for #<User:0x007f8c8e347f48> 

This happens when registering:

 3: <%= simple_form_for(resource, :as => resource_name, :url => registration_path(resource_name), html: { class: 'form-horizontal'}) do |f| %> 4: <%= f.error_notification %> 5: 6: <%= f.input :username %> 7: <%= f.input :email %> 8: <%= f.input :password %> 9: <%= f.input :password_confirmation %> 

This is in user.rb models

 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 configure it, I placed the following commands through the terminal:

 rails generate migration AddUsernameToUsers username:string bundle exec rake db:migrate 

Through the previous questions, I add these commands through the terminal:

 rake db:schema:load 

The error does not even allow me to access the page. Unlike other questions, when does this happen after clicking a button.

Edit

After rebooting my server several times, it automatically issues a 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 /view.rb models:

 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: remember_me in /view.rb models and now the server is working. Now I can upload it to localhost: 3000. However, when I click on the registration page, I get the same error as before.

+4
source share
2 answers

If you want to log in using either your username or password, you have a very good explanation here: Come up with a username using your username or email address

If you want to log in using your username only, you will have to change your authentication_key file from the 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 ] 

You will also need to change your registration and session views to match your identifier_name.

In development / registration / new.html.erb:

 3: <%= simple_form_for(resource, :as => resource_name, :url => registration_path(resource_name), html: { class: 'form-horizontal'}) do |f| %> 4: <%= f.error_notification %> 5: 6: <%= f.input :username %> 7: <%= f.input :password %> 8: <%= f.input :password_confirmation %> 9: 10: <%= f.submit "Sign up" %> 

In development / registration / edit.html.erb:

 3: <%= simple_form_for(resource, :as => resource_name, :url => registration_path(resource_name), html: { class: 'form-horizontal'}) do |f| %> 4: <%= f.error_notification %> 5: 6: <%= f.input :username %> 

In development /session/new.html.erb:

 3: <%= simple_form_for(resource, :as => resource_name, :url => registration_path(resource_name), html: { class: 'form-horizontal'}) do |f| %> 4: 5: <%= f.input :username %> 6: <%= f.input :password %> 7: 8: <%= f.submit "Sign in" %> 
+1
source

A little late, but I decided to share my answer when I get the same error. Migration is not enough. You should add this bit of code to your application_controller.rb :

 before_action :configure_permitted_parameters, if: :devise_controller? protected def configure_permitted_parameters devise_parameter_sanitizer.for(:sign_up) << :username end 

This will do the trick.

+1
source

Source: https://habr.com/ru/post/1491570/


All Articles