使用设计时,当我尝试使用新字段“用户名”加载注册页面时,我不断收到此错误
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 上加载它。但是,当我单击注册页面时,我收到与以前相同的错误。