Ruby on Rails - Field Set to Nil

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:

My Model:

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

My Controller:

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

The View:

<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>

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.

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.

Any ideas? Thanks in advance.

Answers


You've written:

attr_accessor :email, :password, :password_confirmation

Have you tried removing the email parameter from this list? It's probably overriding AR's persistence for the email attribute. You may want attr_accessible instead for email.


Need Your Help

How to get only part of URL from HttpServletRequest?

java string url servlets httprequest

From the following URL I need to get (http://localhost:9090/dts) alone.