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. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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. On Mar 2, 10:54 am, foo bar <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> 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. > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> 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 > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 On Mar 2, 11:21 am, dotjake <dotj...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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 <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > wrote: > > > 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 > > -- > > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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. On Mar 2, 11:29 am, dotjake <dotj...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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 > > On Mar 2, 11:21 am, dotjake <dotj...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > 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 <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > > wrote: > > > > 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 > > > -- > > > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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. On Mar 2, 12:05 pm, james white <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> 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. > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Where would i define a current_user object so that it would be accessible across the application? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Where would i define a current_user object so that it would be > accessible across the application? > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---