2013-07-16 64 views
4

當使用設計我不斷收到這個錯誤,當我嘗試用新的領域來加載註冊頁‘用戶名’Rails的:設計:未定義的方法`用戶名」

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 

要設置它,我把下面的命令通過TERMI最終:

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) 

編輯

這是模型/視圖:

編輯

重新啓動我的服務器幾次,現在是自動exiitng與此錯誤的本地服務器之後.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 

編輯

我刪除逗號在結束:在模型/ view.rb remember_me現在服務器工作。我現在可以在localhost:3000上加載它。但是,當我點擊註冊頁面時,我收到了和以前一樣的錯誤。

+0

如果'resource'不是'User'的實例,那麼你遇到了很大的麻煩。 – oldergod

+0

我剛開始學習Rails,我該如何檢查是否是這種情況,以及如何解決這個問題?謝謝! –

+0

遷移後是否重新啓動了服務器? – Debadatt

回答

1

如果您想在登錄使用或者您的用戶名或密碼,你有一個很好的解釋在這裏:Devise login using your username or email address

如果你只想使用您的用戶名登錄,您必須將您authentication_key從色器件改變.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 ] 

您還需要根據您的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" %> 
+0

那麼這是我的問題的解決方案?如果我這樣做會工作嗎? –

+0

我經歷了整個演練,並沒有工作,當我得到:用戶名它給了我同樣的錯誤。 '未定義的方法「用戶名」爲#<用戶:0x007ff1ee1a0648>' –

+0

你改變根據您的authentication_key的設計有何看法? – ma3x

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 

這將這樣的伎倆。

相關問題