Ruby on rails Rails:designe:Undefined方法`username';
在使用Desive时,当我尝试加载带有新字段“username”的注册页面时,我不断遇到此错误 通过前面的问题,我通过终端发送了以下命令:Ruby on rails Rails:designe:Undefined方法`username';,ruby-on-rails,ruby,devise,Ruby On Rails,Ruby,Devise,在使用Desive时,当我尝试加载带有新字段“username”的注册页面时,我不断遇到此错误 通过前面的问题,我通过终端发送了以下命令: rake db:schema:load 该错误甚至不允许我访问该页面。与单击“注册”后出现的其他问题不同 编辑 重新启动服务器几次后,它现在会自动退出本地服务器,并出现以下错误: Called from: /usr/local/rvm/gems/ruby-1.9.3-p194/gems/actionpack- 3.2.8/lib/action_di
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中删除了:memory_me结尾的逗号,现在服务器可以工作了。我现在可以在localhost:3000上加载它了。然而,当我点击注册页面时,我得到了与以前相同的错误 如果您想使用用户名或密码登录,这里有一个很好的解释: 如果只想使用用户名登录,则必须从designe.rb配置文件中更改身份验证密钥:
# ==> 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 ]
此外,您还必须根据身份验证密钥修改注册和会话视图
在design/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" %>
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 %>
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" %>
3:resource_name,:url=>registration_path(resource_name),html:{class:'form horizontal'})do | f |%>
4:
5:
6:
7:
8:
9:
10:
在designe/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 %>
7: <%= f.input :password %>
8: <%= f.input :password_confirmation %>
9:
10: <%= f.submit "Sign up" %>
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 %>
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" %>
3:resource_name,:url=>registration_path(resource_name),html:{class:'form horizontal'})do | f |%>
4:
5:
6:
在designe/sessions/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" %>
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 %>
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" %>
3:resource_name,:url=>registration_path(resource_name),html:{class:'form horizontal'})do | f |%>
4:
5:
6:
7:
8:
有点晚了,但我想分享我的答案,因为我遇到了同样的错误。移民是不够的。您需要将此代码位添加到应用程序\u controller.rb
:
before_action :configure_permitted_parameters, if: :devise_controller?
protected
def configure_permitted_parameters
devise_parameter_sanitizer.for(:sign_up) << :username
end
在\u操作之前:配置\u允许的\u参数,如果::设计\u控制器?
受保护的
def配置\u允许的\u参数
设计参数消毒剂。对于(:注册)如果resource
不是User
的一个实例,你就有大麻烦了。我刚开始学习Rails,我该如何检查这种情况以及如何修复它?谢谢迁移后是否重新启动了服务器?您正在尝试使用用户名作为身份验证。密钥而不是默认值(电子邮件)?向我们显示模型/视图的内容。那么这是我问题的解决方案吗?如果我这样做,它会工作吗?我完成了整个演练,但它没有工作,当我到达:username时,它给了我相同的错误<代码>未定义的#
方法“username”是否根据您的身份验证密钥更改了设备视图?新手问题,我该怎么做?您必须从应用程序中更改以下视图:design/sessions/new.html.erb、design/registrations/edit.html.erb、design/registrations/new.html.erb。我将根据此编辑我的答案。