Ruby on rails RubyonRails-字段设置为Nil

Ruby on rails RubyonRails-字段设置为Nil,ruby-on-rails,ruby,validation,Ruby On Rails,Ruby,Validation,为我的RubyonRails应用程序处理一个简单的自定义身份验证部分。我试图在用户注册应用程序时要求发送电子邮件,但当我尝试注册过程时,会在数据库中创建一条记录,但电子邮件设置为零。下面是一些代码: 我的模型: class User < ActiveRecord::Base attr_accessor :email, :password, :password_confirmation before_save :encrypt validates :password,

为我的RubyonRails应用程序处理一个简单的自定义身份验证部分。我试图在用户注册应用程序时要求发送电子邮件,但当我尝试注册过程时,会在数据库中创建一条记录,但电子邮件设置为零。下面是一些代码:

我的模型:

class User < ActiveRecord::Base  
 attr_accessor :email, :password, :password_confirmation
 before_save :encrypt

 validates :password,
           :presence => true,
           :confirmation => true
 validates :email,
        :presence => true,
        :uniqueness => true,
        :format => { :with => /^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}\z$/ }

 def encrypt
   if password.present?
     self.password_salt = BCrypt::Engine.generate_salt
     self.password_hash = BCrypt::Engine.hash_secret(password, password_salt)
   end
 end

 def self.authenticate(email, password)
   user = find_by_email(email)
   if user && user.password_hash = BCrypt::Engine.hash_secret(password, user.password_salt)
     user
   else
     nil
   end
 end
end
class用户true,
:确认=>true
验证:电子邮件,
:presence=>true,
:唯一性=>true,
:format=>{:with=>/^[A-Za-z0-9.\%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}\z$/}
def加密
如果密码存在?
self.password\u salt=BCrypt::Engine.generate\u salt
self.password\u hash=BCrypt::Engine.hash\u secret(密码、密码)
结束
结束
def自我验证(电子邮件、密码)
用户=通过电子邮件查找(电子邮件)
如果user&&user.password\u hash=BCrypt::Engine.hash\u secret(密码,user.password\u salt)
用户
其他的
无
结束
结束
结束
我的控制器:

class UsersController < ApplicationController
  skip_filter :login_required, :only => [:create, :new]

  def new
    @user = User.new
    render :layout => 'unauthenticated'
  end

  def create
    @user = User.new(params[:user])
    @user.last_login = DateTime.now
    @user.is_active = true

    if @user.save
      session[:user_id] = @user.id

      redirect_to root_url
    else
      render :action => :new
    end
  end

end
class UsersController[:创建,:新建]
def新
@user=user.new
呈现:布局=>'unauthenticated'
结束
def创建
@user=user.new(参数[:user])
@user.last_login=DateTime.now
@user.is_active=true
如果@user.save
会话[:user\u id]=@user.id
将\u重定向到根\u url
其他的
呈现:操作=>:新建
结束
结束
结束
观点:

<div id="register">
  <%= form_for @user do |f| %>
  <% if @user.errors.any? %>
    <div class="error">
      <ul>
        <% for message in @user.errors.full_messages %>
        <li><%= message %></li>
        <% end %>
      </ul>
    </div>
    <% end %>
    <ul>
      <li>
        <%= f.label :email %>
        <%= f.text_field :email %>
      </li>
      <li>
        <%= f.label :password %>
        <%= f.password_field :password %>
      </li>
      <li>
        <%= f.label :password_confirmation %>
        <%= f.password_field :password_confirmation %>
      </li>
      <li>
        <%= f.submit 'Register' %>
      </li>
    </ul>
  <% end %>
</div>

无论出于何种原因,每次用户注册时,电子邮件都会设置为零。看起来它处理电子邮件的唯一方法是视图上的字段和验证,所以我不知道验证是否正在剥离它,并且没有抛出错误

:login\u required方法位于我的应用程序\u控制器中,是一种检查,以确保用户已登录会话。skip_筛选器用于在进入登录和注册页面时不检查

有什么想法吗?提前感谢。

您已经写了:

attr_accessor :email, :password, :password_confirmation

您是否已尝试从此列表中删除电子邮件参数?它可能会覆盖电子邮件属性的AR持久性。您可能希望电子邮件可以访问
attr\u

在创建方法的开头检查
params
变量。这样做了。我从列表中删除了:电子邮件,它成功了。我对RubyonRails有点陌生,所以让我确定我的答案是正确的。根据我所读的内容,attr_accessor为指定的字段创建get和set方法。attr_accessible只允许控制器通过模型访问该字段?attr_accessior是Ruby的缩写,用于为实例(@)变量的任何类定义getter和setter方法。ActiveRecord会根据模型上的属性自动创建这些属性,因此您很少使用它。是的,attr_accessible是一个基本的安全措施,这意味着变量不会被设置为update_属性或类似的方法。太棒了,谢谢你的帮助。来自.Net的Ruby还有很多细微差别需要学习。