设计...添加列到用户模型不验证 [英] Devise... additional columns to User model not validating

查看:84
本文介绍了设计...添加列到用户模型不验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Rails 3应用程序中使用Devise进行身份验证。我通过db:migrate将first_name和last_name列添加到数据库。注册表单如下修改:

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

< p><%= f.label:first_name%>< br />
<%= f.text_field:first_name%>< / p>

< p><%= f.label:last_name%>< br />
<%= f.text_field:last_name%>< / p>

< p><%= f.label:email%>< br />
<%= f.text_field:email%>< / p>

< p><%= f.label:password%>< br />
<%= f.password_field:password%>< / p>

< p><%= f.label:password_confirmation%>< br />
<%= f.password_field:password_confirmation%>< / p>

< p><%= f.submit注册%>< / p>
<%end%>

用户模型中的验证:

 验证:first_name,:last_name,:email,:password,:password_confirmation,:presence =>真的

由于某种原因,我得到名字不能为空和姓氏不为空白验证错误。有趣的是,除了股票Devise字段之外,这两个字段是添加的。 Devise是否阻止向User模型添加其他字段?还有什么其他原因可以吗?



谢谢。

解决方案

您的用户模型已定义了attr_accessible。如果这是真的,那么你必须添加:first_name和:last_name到列表。


I am using Devise for authentication in a Rails 3 app. I added first_name and last_name columns to the database via db:migrate. The registration form was modified like so:

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

  <p><%= f.label :first_name %><br />
  <%= f.text_field :first_name %></p>

  <p><%= f.label :last_name %><br />
  <%= f.text_field :last_name %></p>

  <p><%= f.label :email %><br />
  <%= f.text_field :email %></p>

  <p><%= f.label :password %><br />
  <%= f.password_field :password %></p>

  <p><%= f.label :password_confirmation %><br />
  <%= f.password_field :password_confirmation %></p>

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

Validation in the User model:

  validates :first_name, :last_name, :email, :password, :password_confirmation, :presence => true

For some reason I am getting "First name can't be blank" and "Last name can't be blank" validation errors. It is interesting to note that these are the two fields added in addition to the stock Devise fields. Does Devise prevent adding other fields to the User model? What other reason could there be?

Thanks.

解决方案

Probably your user model has defined attr_accessible. if this is true then you have to add :first_name and :last_name to the list.

这篇关于设计...添加列到用户模型不验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆