title: Ruby on Rails - Field Set to Nil
body: <p>Working a simple custom authentication portion for my Ruby on Rails application. I'm trying to make email required when the user registers with the app but when I try the registration process, a record is created in the database but email is set to nil. Here's some code:</p>
  
  <p>My Model:</p>
  
  <pre><code>class User &lt; ActiveRecord::Base  
   attr_accessor :email, :password, :password_confirmation
   before_save :encrypt
  
   validates :password,
             :presence =&gt; true,
             :confirmation =&gt; true
   validates :email,
          :presence =&gt; true,
          :uniqueness =&gt; true,
          :format =&gt; { :with =&gt; /^[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 &amp;&amp; user.password_hash = BCrypt::Engine.hash_secret(password, user.password_salt)
       user
     else
       nil
     end
   end
  end
  </code></pre>
  
  <p>My Controller:</p>
  
  <pre><code>class UsersController &lt; ApplicationController
    skip_filter :login_required, :only =&gt; [:create, :new]
  
    def new
      @user = User.new
      render :layout =&gt; '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 =&gt; :new
      end
    end
  
  end
  </code></pre>
  
  <p>The View:</p>
  
  <pre><code>&lt;div id="register"&gt;
    &lt;%= form_for @user do |f| %&gt;
    &lt;% if @user.errors.any? %&gt;
      &lt;div class="error"&gt;
        &lt;ul&gt;
          &lt;% for message in @user.errors.full_messages %&gt;
          &lt;li&gt;&lt;%= message %&gt;&lt;/li&gt;
          &lt;% end %&gt;
        &lt;/ul&gt;
      &lt;/div&gt;
      &lt;% end %&gt;
      &lt;ul&gt;
        &lt;li&gt;
          &lt;%= f.label :email %&gt;
          &lt;%= f.text_field :email %&gt;
        &lt;/li&gt;
        &lt;li&gt;
          &lt;%= f.label :password %&gt;
          &lt;%= f.password_field :password %&gt;
        &lt;/li&gt;
        &lt;li&gt;
          &lt;%= f.label :password_confirmation %&gt;
          &lt;%= f.password_field :password_confirmation %&gt;
        &lt;/li&gt;
        &lt;li&gt;
          &lt;%= f.submit 'Register' %&gt;
        &lt;/li&gt;
      &lt;/ul&gt;
    &lt;% end %&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>For whatever reason the email gets set to nil every time a user is registered. The only that looks like it deals with the email is the field on the view, and the validation so I don't know if maybe the validation is stripping it and no error is being thrown.</p>
  
  <p>The :login_required method sits in my application_controller and is a check to make sure the user is logged in for the session. The skip_filter is to not check that when going to the sign in and registeration pages.</p>
  
  <p>Any ideas? Thanks in advance.</p>