Basic misunderstanding of syntax i think

First of thank you for your patience. This is my first ruby project and
i am slightly confused. The index page works fine.

<h1>Index</h1>

<p>
        Welcome <%= h(session[:user_name]) %>

        <%= error_messages_for 'user' %>
</p>

<% if session[:user_id] == nil %>

<% form_tag :controller => 'users', :action => 'login' do %>
      <p>
        <label for="name">Name:</label>
        <%= text_field_tag :name, params[:name] %>
      </p>

      <p>
        <label for="password">Password:</label>
        <%= password_field_tag :password, params[:password] %>
      </p>

      <p>
        <%= submit_tag "Login" %>
      </p>
    <% end %>

<% else %>

<%= button_to "logout", :controller => 'users', :action => 'logout' %>

<% end %>

and then when i go to /users/list_friends the following code displays
fine

<h1>Your Friends Are</h1>
<% if flash[:notice] -%>
      <div id="notice"><%= flash[:notice] %></div>
    <% end -%>
<ul>
  <% for user in @my_friends %>
    <li>
        <%= h(user.name) %>
    </li>
  <% end %>
</ul>

Yet when i add the code from list friends to the index page it all falls
over. I think i am missing some basic understanding of what is going on
here. The code for the list friends is.

def list_friends

    if session[:user_id]
     myuser = User.find(session[:user_id])
     @my_friends = myuser.friends
    else
    flash[:notice] = "Please Login"
    end

end

Thank you again for any help.

It looks like the problem you might be having is that you create an
object, @my_friends, in your action list_friends. If you want to call
this object on your index page, you would need to recreate it in the
index action of your controller.

Is it possible for me to call the function from the index page? or will
i have to put all the functions inside index?

thanks

james

You'll need to create @my_friends in the index action.

You might want to consider creating a current_user object across your
application. Check out the acts_as_authenticated plugin (http://
technoweenie.stikipad.com/plugins/show/Acts+as+Authenticated), it has
pretty much everything you'll need.

Regards
Jake

Using Acts as Authenticated, you can set a before_filter on specific
actions in your controller that checks if a user is logged in, and if
not, it redirects them to a login page. That way you can take the
logic for logging in out of your index page.

Then, you can simplify your actions to be something like

before_filter :login_required, :only => [:index, :list_friends]
before_filter :get_friends, :only => [:index, :list_friends]

def index
end

def list_friends
end

protected

def get_friends
  @my_friends = current_user.friends
end

This is more the 'rails-way' because it is DRY - don't repeat
yourself.

Regards

And, if you want to be even more DRY, extract the code for listing
friends into a partial:

<ul>
  <% for user in @my_friends %>
    <li>
        <%= h(user.name) %>
    </li>
  <% end %>
</ul>

that you call on index and list_friends.

You can also put your flash notice:

<% if flash[:notice] -%>
      <div id="notice"><%= flash[:notice] %></div>
<% end -%>

into your view layout template.

Great! If you're just getting started with Rails, I highly recommend
getting the book Agile Web Development with Rails.

I used it when I got started and it proves to be an invaluable
resource.

Where would i define a current_user object so that it would be
accessible across the application?

You can define it in your application controller, in /app/controllers/
application.rb, via a method like

    def current_user
      @current_user ||= (session[:user] &&
User.find_by_id(session[:user])) || :false
    end

Acts as Authenticated will set up current_user for you, and it's
really easy to implement, there's a generate script that adds a file
to your /lib folder. Even if you don't want to use it, it will give
you a good idea of rails best practices for dealing with
authentication.

You're right, Rails books sometimes seem out-of-date because the
framework evolves pretty rapidly, but many of the same practices used
in the 1.2 book are still in use today, in addition to new practices
like greater emphasis on RESTful apps, sexy migrations, etc.