Hi, When users are logged in, they should always have a ''Logout'' function handy somewhere. Users control Forms and Elements using two controllers, FormsController and ElementsController. What at least works is to add the following lines to the end of both layout files. views/layouts/elements.erb.html and views/layouts/forms.erb.html <hr /> <%= link_to "Logout #{User.find(session[:user_id]).name}", :controller => ''admin'', :action => ''logout'' %> </body> </html> This gives me two issues. - I''m putting business logic in the View. - I''m putting this line across two files. CmdJohnson --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Put it in a layout (app/views/layout) - if logged_in? = link_to "Logout #{current_user.login}", logout_path These layouts are used when a controller is told to use them via: layout ''layoutname'' On Sat, Sep 20, 2008 at 5:09 PM, Commander Johnson <commanderjohnson-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi, > When users are logged in, they should always have a ''Logout'' function handy > somewhere. > Users control Forms and Elements using two controllers, FormsController and > ElementsController. > What at least works is to add the following lines to the end of both layout > files. > views/layouts/elements.erb.html and > views/layouts/forms.erb.html > > <hr /> > <%= link_to "Logout #{User.find(session[:user_id]).name}", :controller => > ''admin'', :action => ''logout'' %> > </body> > </html> > This gives me two issues. > - I''m putting business logic in the View. > - I''m putting this line across two files. > CmdJohnson > > >-- Ramon Tayag --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
On Sep 20, 5:09 pm, "Commander Johnson" <commanderjohn...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi, > When users are logged in, they should always have a ''Logout'' function handy > somewhere. > > Users control Forms and Elements using two controllers, FormsController and > ElementsController. > > What at least works is to add the following lines to the end of both layout > files. > > views/layouts/elements.erb.html and > views/layouts/forms.erb.html > > <hr /> > <%= link_to "Logout #{User.find(session[:user_id]).name}", :controller => > ''admin'', :action => ''logout'' %> > </body> > </html> > > This gives me two issues. > - I''m putting business logic in the View. > - I''m putting this line across two files. > > CmdJohnsonI agree with Ramon. Also, I suggest you put the following in your application.rb: before_filter :maintain_session protected def maintain_session @current_user = User.find(session[:user_id]) end That way, you can do this on your layout: <%= link_to "Logout #{@current_user.name}", :controller => ''admin'', :action => ''logout'' %> --~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Erol, Thanks for that snippet. This is what I made of it: def maintain_session if session[:user_id] @current_user = User.find(session[:user_id]) end end Otherwise an error occurs before login. Ramon, I don''t understand yet how this will apply to both the FormsController and ElementsController. On Sat, Sep 20, 2008 at 12:49 PM, Erol Fornoles <erol.fornoles-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>wrote:> > On Sep 20, 5:09 pm, "Commander Johnson" <commanderjohn...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > wrote: > > Hi, > > When users are logged in, they should always have a ''Logout'' function > handy > > somewhere. > > > > Users control Forms and Elements using two controllers, FormsController > and > > ElementsController. > > > > What at least works is to add the following lines to the end of both > layout > > files. > > > > views/layouts/elements.erb.html and > > views/layouts/forms.erb.html > > > > <hr /> > > <%= link_to "Logout #{User.find(session[:user_id]).name}", :controller => > > ''admin'', :action => ''logout'' %> > > </body> > > </html> > > > > This gives me two issues. > > - I''m putting business logic in the View. > > - I''m putting this line across two files. > > > > CmdJohnson > > I agree with Ramon. Also, I suggest you put the following in your > application.rb: > > before_filter :maintain_session > > protected > > def maintain_session > @current_user = User.find(session[:user_id]) > end > > That way, you can do this on your layout: > > <%= link_to "Logout #{@current_user.name}", :controller => > ''admin'', :action => ''logout'' %> > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Many apps use app/views/layouts/application.html.erb as a layout that applies to the website globally. If you do want separate layouts for your two controllers, I would create a logout partial. You could do it by making a file app/views/shared/_logout.html.erb that contains your logout link. Then from your two other layouts you would have to do: <%= render :partial => "shared/logout" %> Regarding the controller, you would need to put the suggested before_filter in the application.rb controller so that it runs for any controller. I would expand the logic a little bit. Probably something like this: class ApplicationController before_filter :find_current_user def find_current_user @current_user = User.find(session[:user_id]) if session[:user_id] end def logged_in? !session[:user_id].nil? end helper_method :logged_in? end Then in your partial: <% if logged_in? %> <%= link_to "log out #{@current_user.name}", logout_path <% end %> For logout_path to be available, you''ll need to be using named routes. If you''re not comfortable doing that in Rails yet, you could use :controller => ''admin'', :action => ''logout'' like you originally had. -Dan Manges http://www.dcmanges.com/blog On Sep 20, 6:36 am, "Commander Johnson" <commanderjohn...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Erol, > Thanks for that snippet. This is what I made of it: > > def maintain_session > if session[:user_id] > @current_user = User.find(session[:user_id]) > end > end > > Otherwise an error occurs before login. > > Ramon, > > I don''t understand yet how this will apply to both the FormsController and > ElementsController. > > On Sat, Sep 20, 2008 at 12:49 PM, Erol Fornoles <erol.forno...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>wrote: > > > > > On Sep 20, 5:09 pm, "Commander Johnson" <commanderjohn...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > > wrote: > > > Hi, > > > When users are logged in, they should always have a ''Logout'' function > > handy > > > somewhere. > > > > Users control Forms and Elements using two controllers, FormsController > > and > > > ElementsController. > > > > What at least works is to add the following lines to the end of both > > layout > > > files. > > > > views/layouts/elements.erb.html and > > > views/layouts/forms.erb.html > > > > <hr /> > > > <%= link_to "Logout #{User.find(session[:user_id]).name}", :controller => > > > ''admin'', :action => ''logout'' %> > > > </body> > > > </html> > > > > This gives me two issues. > > > - I''m putting business logic in the View. > > > - I''m putting this line across two files. > > > > CmdJohnson > > > I agree with Ramon. Also, I suggest you put the following in your > > application.rb: > > > before_filter :maintain_session > > > protected > > > def maintain_session > > @current_user = User.find(session[:user_id]) > > end > > > That way, you can do this on your layout: > > > <%= link_to "Logout #{@current_user.name}", :controller => > > ''admin'', :action => ''logout'' %>--~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---