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.

Index

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

    <%= error_messages_for 'user' %>

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

<% form_tag :controller => ‘users’, :action => ‘login’ do %>


Name:
<%= text_field_tag :name, params[:name] %>

  <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

Your Friends Are

<% if flash[:notice] -%>
<%= flash[:notice] %>
<% end -%>
    <% for user in @my_friends %>
  • <%= h(user.name) %>
  • <% end %>

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

On Mar 2, 11:11 am, james white [email protected]

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:

    <% for user in @my_friends %>
  • <%= h(user.name) %>
  • <% end %>

that you call on index and list_friends.

You can also put your flash notice:

<% if flash[:notice] -%>

<%= flash[:notice] %>

<% end -%>

into your view layout template.

Thank you for your help.

I am still trying to get an understanding of the basics of rails and how
it all works. After adding

def index
@my_friends = list_friends
end

It all seems to work.

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

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

On Mar 2, 12:05 pm, james white [email protected]

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

I have the book but it has been difficult to follow along with as its
not 2.0. I did not see the point trying to learn 1.2 when it seems rails
is changing so much.

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.

On Mar 2, 12:39 pm, james white [email protected]