Rails:设计:未定义的方法`用户名'

时间:2013-07-16 05:16:47

标签: ruby-on-rails ruby devise

使用设计时,当我尝试使用新字段“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上加载它。但是,当我点击注册页面时,我得到了与以前相同的错误。

2 个答案:

答案 0 :(得分:1)

如果您想使用您的用户名或密码登录,您可以在此处获得非常好的解释: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" %>

答案 1 :(得分:1)

现在有点迟了但我想分享我的答案,因为我得到了同样的错误。迁移是不够的。您需要将这段代码添加到application_controller.rb

before_action :configure_permitted_parameters, if: :devise_controller?

  protected

  def configure_permitted_parameters
    devise_parameter_sanitizer.for(:sign_up) << :username
end

这样就可以了。