添加自定义字段以设计注册 - Ruby on Rails 4&Devise 3

eNN*_*eNN 0 ruby-on-rails devise

嗨,我有一个模型Coach,我正在尝试添加特定的字段.我创建了一个迁移并将字段添加到注册中,但在Devise :: Registrations #new&undefined method`first_name'中收到错误 NoMethodError

谢谢你看看这个...
更新:我已经迁移了数据库这是我的代码到目前为止的样子:

注册

注册成为教练

<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
  <%= devise_error_messages! %>


  <div><%= f.label :first_name %><br />
  <%= f.text_field :first_name, autofocus: true %></div>

  <div><%= f.label :email %><br />
  <%= f.email_field :email, autofocus: true %></div>

  <div><%= f.label :password %><br />
    <%= f.password_field :password, autocomplete: "off" %></div>

  <div><%= f.label :password_confirmation %><br />
    <%= f.password_field :password_confirmation, autocomplete: "off" %></div>

  <div><%= f.submit "Sign up" %></div>
<% end %>

<%= render "coaches/shared/links" %>
Run Code Online (Sandbox Code Playgroud)

移民

class AddFieldsToCoaches < ActiveRecord::Migration
  def change
    add_column :users, :first_name, :string
    add_column :users, :last_name, :string
  end
end
Run Code Online (Sandbox Code Playgroud)

架构

ActiveRecord::Schema.define(version: 20140627044906) do

  create_table "coaches", force: true do |t|
    t.datetime "created_at"
    t.datetime "updated_at"
    t.string   "email",                  default: "", null: false
    t.string   "encrypted_password",     default: "", null: false
    t.string   "reset_password_token"
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    t.integer  "sign_in_count",          default: 0,  null: false
    t.datetime "current_sign_in_at"
    t.datetime "last_sign_in_at"
    t.string   "current_sign_in_ip"
    t.string   "last_sign_in_ip"
  end

  add_index "coaches", ["email"], name: "index_coaches_on_email", unique: true
  add_index "coaches", ["reset_password_token"], name: "index_coaches_on_reset_password_token", unique: true

  create_table "players", force: true do |t|
    t.datetime "created_at"
    t.datetime "updated_at"
    t.string   "email",                  default: "", null: false
    t.string   "encrypted_password",     default: "", null: false
    t.string   "reset_password_token"
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    t.integer  "sign_in_count",          default: 0,  null: false
    t.datetime "current_sign_in_at"
    t.datetime "last_sign_in_at"
    t.string   "current_sign_in_ip"
    t.string   "last_sign_in_ip"
  end

  add_index "players", ["email"], name: "index_players_on_email", unique: true
  add_index "players", ["reset_password_token"], name: "index_players_on_reset_password_token", unique: true

  create_table "users", force: true do |t|
    t.string   "email",                  default: "", null: false
    t.string   "encrypted_password",     default: "", null: false
    t.string   "reset_password_token"
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    t.integer  "sign_in_count",          default: 0,  null: false
    t.datetime "current_sign_in_at"
    t.datetime "last_sign_in_at"
    t.string   "current_sign_in_ip"
    t.string   "last_sign_in_ip"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  add_index "users", ["email"], name: "index_users_on_email", unique: true
  add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true

end
Run Code Online (Sandbox Code Playgroud)

回溯

Started GET "/coaches/sign_up" for 127.0.0.1 at 2014-06-27 00:50:41 -0400
Processing by Devise::RegistrationsController#new as HTML
  Rendered coaches/registrations/new.html.erb within layouts/application (1.7ms)
Completed 500 Internal Server Error in 6ms

ActionView::Template::Error (undefined method `first_name' for #<Coach:0x007f9f874d6510>):
     5:     
     6:     
     7:   <div><%= f.label :first_name %><br />
     8:   <%= f.text_field :first_name, autofocus: true %></div>
     9:   
    10:   <div><%= f.label :email %><br />
    11:   <%= f.email_field :email, autofocus: true %></div>
  app/views/coaches/registrations/new.html.erb:8:in `block in _app_views_coaches_registrations_new_html_erb__1603382164571940584_70161540751300'
  app/views/coaches/registrations/new.html.erb:3:in `_app_views_coaches_registrations_new_html_erb__1603382164571940584_70161540751300'


  Rendered /Users/noahreisch/.rvm/gems/ruby-2.1.1/gems/actionpack-4.1.0/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (1.3ms)
  Rendered /Users/noahreisch/.rvm/gems/ruby-2.1.1/gems/actionpack-4.1.0/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (0.9ms)
  Rendered /Users/noahreisch/.rvm/gems/ruby-2.1.1/gems/actionpack-4.1.0/lib/action_dispatch/middleware/templates/rescues/template_error.html.erb within rescues/layout (9.8ms)
Run Code Online (Sandbox Code Playgroud)

Max*_*nin 5

强参数可能有问题.尝试将以下代码添加到application_controller.rb.

class ApplicationController < ActionController::Base
  before_action :configure_permitted_parameters, if: :devise_controller?

  protected

  def configure_permitted_parameters
    devise_parameter_sanitizer.for(:sign_up) << :first_name
  end
end
Run Code Online (Sandbox Code Playgroud)

有关更多信息,请访问:https://github.com/plataformatec/devise#strong-parameters