我尝试将字段"name"
添加到Devise中,但是当我注册新用户时,它会在我提交提交时中断。
我已确保运行rake db:migrate
并且我已重启我的服务器,但我收到错误:
Devise中的ActiveRecord :: StatementInvalid :: RegistrationsController #create
NoMethodError:未定义的方法`name' for nil:NilClass:INSERT INTO "用户" (" created_at"," current_sign_in_at"," current_sign_in_ip", "电子邮件"," encrypted_password"," last_sign_in_at"," last_sign_in_ip", " name"," pic_file_name"," remember_created_at", " reset_password_sent_at"," reset_password_token"," sign_in_count", " title"," updated_at")VALUES($ 1,$ 2,$ 3,$ 4,$ 5,$ 6,$ 7,$ 8,$ 9, $ 10,$ 11,$ 12,$ 13,$ 14,$ 15)返回" id"
用户类
class User < ActiveRecord::Base
has_and_belongs_to_many :roles
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
attr_accessible :email, :password, :password_confirmation, :remember_me, :role_ids, :name, :title, :pic_file_name
has_one :picture, :dependent => :destroy
accepts_nested_attributes_for :picture, :allow_destroy => true
has_many :comments, :dependent => :destroy
has_many :discussions, :dependent => :destroy
def role?(role)
roles.map(&:name).include? role.to_s
end
end
模式
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
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", :null => false
t.datetime "updated_at", :null => false
t.string "name"
t.string "title"
t.string "pic_file_name"
end
新注册视图
<%= simple_form_for(resource, :as => resource_name, :html => { :multipart => true }, :html => { :class => 'form-horizontal' }, :url => registration_path(resource_name)) do |f| %>
<%= f.error_notification %>
<div class="row">
<div class="span12 rounded-corners shadow inputs registration">
<div class="row">
<div class="span6">
<h1>Join the Club</h1>
<%= f.input :email, :required => true, :autofocus => true, :input_html => { :class => "span4" } %>
<%= f.input :password, :required => true, :input_html => { :class => "span4" } %>
<%= f.input :password_confirmation, :required => true, :input_html => { :class => "span4" } %>
<%= f.input :name, :required => true, :input_html => { :class => "span4" } %>
<%= f.input :title, :required => true, :input_html => { :class => "span4" } %>
<%= f.input :pic_file_name, :as => :file, :label => "Upload Profile Picture", :required => true %>
</div>
<div class="span6 .hidden-phone ">
<%= image_tag "signup.png", :class => "rounded-corners shadow signup_pic" %>
</div>
</div>
<div class="row">
<div class="signup_actions span3">
<%= f.button :submit, "Sign up", :class => 'btn-warning btn-large' %></br >
<%= render "links" %>
</div>
</div>
<% end %>
</div>
答案 0 :(得分:0)
将attr_accessible:
之后的名称放在用户类中。