nobosh
2010-Sep-03 23:19 UTC
Action Controller Error: undefined local variable or method `current_user''
Newbie learning Rails. I''m currently on Chp9 here: http://railstutorial.org/chapters/sign-in-sign-out#top At the end of the tutorial, rails is erroring (see below). being new to Rails and after having checked the tutorial... How do you resolve this kind of error. It''s saying current_user is not defined, and it is supposed to be defined with/Sites/sample_app/app/helpers/ sessions_helper.rb NameError in Pages#home Showing /Users/iamme/Sites/sample_app/app/views/layouts/ _header.html.erb where line #6 raised: undefined local variable or method `current_user'' for #<#<Class: 0x103371258>:0x10336be70> Extracted source (around line #6): 3: <nav class="round"> 4: <ul> 5: <li><%= link_to "Home", root_path %></li> 6: <% if signed_in? %> 7: <li><%= link_to "Profile", current_user %></li> 8: <% end %> 9: <li><%= link_to "Help", help_path %></li> Trace of template inclusion: app/views/layouts/application.html.erb Rails.root: /Users/iamme/Sites/sample_app Application Trace | Framework Trace | Full Trace app/helpers/sessions_helper.rb:13:in `signed_in?'' app/views/layouts/_header.html.erb:6:in `_app_views_layouts__header_html_erb___1701013666_2174344740_524030'' app/views/layouts/application.html.erb:10:in `_app_views_layouts_application_html_erb__1439570095_2174436720_0'' ---- END sessions_helper.rb CODE: module SessionsHelper def sign_in(user) cookies.permanent.signed[:remember_token] = [user.id, user.salt] current_user = user end def current_user=(user) @current_user = user end def signed_in? !current_user.nil? end def sign_out cookies.delete(:remember_token) self.current_user = nil end private def user_from_remember_token User.authenticate_with_salt(*remember_token) end def remember_token cookies.signed[:remember_token] || [nil, nil] end end --- END thanks -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
nobosh
2010-Sep-03 23:36 UTC
Re: Action Controller Error: undefined local variable or method `current_user''
Ok figured it out, the sessions_helper.rb needs to look like this: module SessionsHelper def sign_in(user) cookies.permanent.signed[:remember_token] = [user.id, user.salt] current_user = user end def current_user=(user) @current_user = user end def current_user @current_user ||= user_from_remember_token end def signed_in? !current_user.nil? end def sign_out cookies.delete(:remember_token) self.current_user = nil end private def user_from_remember_token User.authenticate_with_salt(*remember_token) end def remember_token cookies.signed[:remember_token] || [nil, nil] end end On Sep 3, 4:19 pm, nobosh <bhellm...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Newbie learning Rails. I''m currently on Chp9 here:http://railstutorial.org/chapters/sign-in-sign-out#top > > At the end of the tutorial, rails is erroring (see below). being new > to Rails and after having checked the tutorial... How do you resolve > this kind of error. It''s saying current_user is not defined, and it is > supposed to be defined with/Sites/sample_app/app/helpers/ > sessions_helper.rb > > NameError in Pages#home > > Showing /Users/iamme/Sites/sample_app/app/views/layouts/ > _header.html.erb where line #6 raised: > > undefined local variable or method `current_user'' for #<#<Class: > 0x103371258>:0x10336be70> > Extracted source (around line #6): > > 3: <nav class="round"> > 4: <ul> > 5: <li><%= link_to "Home", root_path %></li> > 6: <% if signed_in? %> > 7: <li><%= link_to "Profile", current_user %></li> > 8: <% end %> > 9: <li><%= link_to "Help", help_path %></li> > Trace of template inclusion: app/views/layouts/application.html.erb > > Rails.root: /Users/iamme/Sites/sample_app > > Application Trace | Framework Trace | Full Trace > app/helpers/sessions_helper.rb:13:in `signed_in?'' > app/views/layouts/_header.html.erb:6:in > `_app_views_layouts__header_html_erb___1701013666_2174344740_524030'' > app/views/layouts/application.html.erb:10:in > `_app_views_layouts_application_html_erb__1439570095_2174436720_0'' > > ---- END > > sessions_helper.rb CODE: > > module SessionsHelper > > def sign_in(user) > cookies.permanent.signed[:remember_token] = [user.id, user.salt] > current_user = user > end > > def current_user=(user) > @current_user = user > end > > def signed_in? > !current_user.nil? > end > > def sign_out > cookies.delete(:remember_token) > self.current_user = nil > end > > private > > def user_from_remember_token > User.authenticate_with_salt(*remember_token) > end > > def remember_token > cookies.signed[:remember_token] || [nil, nil] > end > > end > > --- END > > thanks-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@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.