2014-12-06 53 views
0

好吧,所以我試圖添加一個名稱條目到我的登錄頁面使用設計,這裏是我到目前爲止在視圖/ devise/new.html.erb文件。添加名稱設計的用戶登錄拋出一個NoMethodError

<h2>Sign up</h2> 

<div class="row"> 
    <div class="col-md-8"> 
    <%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %> 
     <%= devise_error_messages! %> 
     <div class="form-group"> 
     <%= f.label :name %> 
     <%= f.text_field :name, autofocus: true, class: 'form-control', placeholder: "Enter name" %> 
     </div> 
     <div class="form-group"> 
     <%= f.label :email %> 
     <%= f.email_field :email, class: 'form-control', placeholder: "Enter email" %> 
     </div> 
     <div class="form-group"> 
     <%= f.label :password %> 
     <%= f.password_field :password, class: 'form-control', placeholder: "Enter password" %> 
     </div> 
     <div class="form-group"> 
     <%= f.label :password_confirmation %> 
     <%= f.password_field :password_confirmation, class: 'form-control', placeholder: "Enter  password confirmation" %> 
     </div> 
     <div class="form-group"> 
     <%= f.submit "Sign up", class: 'btn btn-success' %> 
    </div> 
     <div class="form-group"> 
     <%= render "devise/shared/links" %> 
     </div> 
    <% end %> 
    </div> 
</div> 

我知道你必須擴展Devise允許的屬性,我將這個代碼添加到我的應用程序的控制器。

protect_from_forgery with: :exception 
    before_action :configure_permitted_parameters, if: :devise_controller? 

    protected 

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

我也做了一定補充:這裏名更改爲DB /遷移/ devise_create_users

class DeviseCreateUsers < ActiveRecord::Migration 
    def change 
    create_table(:users) do |t| 
     ## Customization 
     t.string :name 
     ## Database authenticatable 
     t.string :email,    null: false, default: "" 
     t.string :encrypted_password, null: false, default: "" 

我確信耙分貝:遷移和db:復位,但我發現了這個錯誤,這對我沒有意義。

Started GET "https://stackoverflow.com/users/sign_up" for 127.0.0.1 at 2014-12-06 15:07:31 +0300 
    ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations" 
Processing by Devise::RegistrationsController#new as HTML 
    Rendered devise/registrations/new.html.erb within layouts/application (235.8ms) 
Completed 500 Internal Server Error in 251ms 

ActionView::Template::Error (undefined method `name' for #<User:0x007fe82225b928>): 
6:  <%= devise_error_messages! %> 
7:  <div class="form-group"> 
8:   <%= f.label :name %> 
9:   <%= f.text_field :name, autofocus: true, class: 'form-control', placeholder: "Enter name" %> 
10:  </div> 
11:  <div class="form-group"> 
12:   <%= f.label :email %> 
    app/views/devise/registrations/new.html.erb:9:in `block in  _app_views_devise_registrations_new_html_erb__738282397383517404_70317490899320' 
    app/views/devise/registrations/new.html.erb:5:in `_app_views_devise_registrations_new_html_erb__738282397383517404_70317490899320' 


Rendered /Users/clydiscope/.rvm/gems/ruby-2.1.5/gems/actionpack- 4.1.8/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (0.9ms) 
Rendered /Users/clydiscope/.rvm/gems/ruby-2.1.5/gems/actionpack-4.1.8/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (0.9ms) 
Rendered /Users/clydiscope/.rvm/gems/ruby-2.1.5/gems/actionpack-4.1.8/lib/action_dispatch/middleware/templates/rescues/template_error.html.erb within rescues/layout (7.3ms) 

任何人都可以幫忙嗎?

+0

你你第一次遷移之後編輯遷移的文件? – 2014-12-06 13:19:08

+0

不,但我在編輯並保存文件後多次遷移。 – 2014-12-06 13:33:31

回答

1

看起來像這裏的問題是你編輯你的一個遷移文件並重新運行遷移。創建遷移文件並運行rake db:migrate後,除非回滾到該特定文件,否則無法編輯該文件

在您的情況下,您可以創建一個新遷移,將name列添加到users表中。

$ rails g migration add_name_to_users 

然後編輯文件。

def up 
    add_column :users, :name, :string 
end 

def down 
    remove_column :users, :name 
end 

現在你可以運行rake db:migrate

+0

Yay成功!!!非常感謝! – 2014-12-06 14:10:38

+0

每個遷移都會永久保留在應用程序中還是可以整合? – 2014-12-06 14:18:50

+0

很高興你現在正在運行!他們可以鞏固,只是非常小心。這是一個很好的答案 - http://stackoverflow.com/a/1490578/2456549 – 2014-12-06 14:27:26