Topic: I'm having a tough time figuring out what's nil and what's not

Check out this code for my view:

<h1><%= @member.first_name %> <%= @member.last_name %>'s Profile <% if @member = @current_user -%>(You)<% end -%></h1>
        <div class="subset">
            <h3>Personal Information</h3>
            <ul class="member">
                <li class="phone"><span>Primary Phone:</span> <%= @member.phone %></li>
                <li class="email"><span>Primary Email:</span> <%= @member.email %></li>
                <li class="website">Personal Website: <a href="<% @member.website %>" title="Web site" rel="external-website"><%= @member.website %></a></li>
                <li class="residence">Current Residence: <%= @member.current_city %>, <%= @member.current_state %></li>
                <li class="major">Major(s): <%= @member.major %></li>
                <li class="minor">Minor(s): <%= @member.minor %></li>
                <li class="grad">Graduation Year: <%= @member.graduation_year %></li>
            </ul>
        </div>

And for the controller:

  def index
    @member = User.find_by_login(params[:member])
    @current_user = User.find_by_login(session[:user])
    @page_title = "Carrollink Members - Viewing " + @member.first_name + "'s personal page"
  end

And I get the following error:

You have a nil object when you didn't expect it!
The error occurred while evaluating nil.phone

How in the world is phone nil and first_name, last_name not?! Arg, I've been scratching my head for a good hour on this one.

Re: I'm having a tough time figuring out what's nil and what's not

After some testing, by taking chaning

<% if @member = @current_user -%>(You)<% end -%>

to



<% if @member = session[:user] -%>(You)<% end -%>

It works. So I guess the question is why isn't @current_user = User.find_by_login(session[:user]) working in my controller?

Re: I'm having a tough time figuring out what's nil and what's not

A couple things:

1. you need two equal signs to do a comparison, one equal sign will set the @member variable:

<% if @member == @current_user -%>

2. What does session[:user] contain? In one case you are comparing it as if it were a model, but in another you use it as a login string for finding the current user.

Railscasts - Free Ruby on Rails Screencasts

Re: I'm having a tough time figuring out what's nil and what's not

That's one of my biggest weaknesses right now: I can't seem to figure out when session[:user] is a string I can compare to and when it's a model I can pull from.

i.e. using @member.first_name one time, and then session[:user].first_name a different on the same page because there might be a difference if you are logged in, but looking at a different member's page.

Re: I'm having a tough time figuring out what's nil and what's not

Oh, my session[:user] if officially declared here in my members_controller:

class MembersController < ApplicationController
 
  def authorize
    if User.authenticate(params[:login], params[:password])
      @user = User.find_by_login(params[:login])
      session[:user] = @user
      redirect_to :action => 'index', :member => @user.login
    else
      reset_session
      redirect_to :action => :login
      flash[:error] = "Invalid user or password"
      flash[:message] = "Your account has not been activated yet"
    end
  end

And I'm maintaining it across the app by having this code within the application.rb

  helper_method :current_user
  def current_user
    @current_user ||= User.find_by_login(session[:user])
  end

Re: I'm having a tough time figuring out what's nil and what's not

Looks like you're storing the entire user model in the session. I recommend just storing the login. Like this:

session[:user] = @user.login

Or better yet, the "id" which is guaranteed to be unique.

session[:user] = @user.id

# in application.rb
@current_user ||= User.find_by_id(session[:user])


Now the current_user method should fetch the user properly.

Railscasts - Free Ruby on Rails Screencasts

Re: I'm having a tough time figuring out what's nil and what's not

Yep. That worked nicely. Thanks - I have a much better understanding of the whole model vs. string with sessions and stuff.