ruby-on-rails – Rails:设计:未定义的方法`用户名’

使用设计时,当我尝试使用新字段“username”加载注册页面时,我一直收到此错误

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

这是在注册时的设计:

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 %>

这是在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

要设置它,我通过终端输入以下命令:

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

通过以前的问题,我把这些命令通过终端:

rake db:schema:load

该错误不允许我访问该页面.与点击注册后发生的其他问题不同.

编辑

重新启动我的服务器几次后,它现在自动使用此错误来使用本地服务器:

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)

编辑

这是在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

编辑

我在models / view.rb的末尾删除了逗号:remember_me,现在服务器正常工作.我现在可以在localhost:3000上加载它.但是,当我点击注册页面时,我得到了与以前相同的错误.

最佳答案 如果您想使用您的用户名或密码登录,那么您在这里有一个非常好的解释:
Devise login using your username or email address

如果您只想使用用户名登录,则必须从devise.rb配置文件中更改authentication_key:

  # ==> 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 ]

此外,您还必须根据authentication_key修改注册和会话视图.

在devise / registrations / 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" %>

在devise / registrations / 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 %>

在devise / sessions / 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" %>
点赞