4

使用设计时,当我尝试使用新字段“用户名”加载注册页面时,我不断收到此错误

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)

编辑

这是在模型/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 上加载它。但是,当我单击注册页面时,我收到与以前相同的错误。

4

2 回答 2

1

如果您想使用您的用户名或密码登录,您在这里有一个很好的解释:使用您的用户名或电子邮件地址设计登录

如果您只想使用您的用户名登录,则必须从您的 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 修改您的注册和会话视图。

在设计/注册/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" %>

在设计/注册/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 %>

在设计/会话/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" %>
于 2013-07-16T17:19:39.390 回答
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

这会成功的。

于 2014-10-06T21:51:57.763 回答