Search code examples
ruby-on-railsnotepad++nomethoderror

Ruby on Rails - NoMethodError in Pages#home


I'm new to rails and am trying to add a sign in function to a web page. I have followed an example a lecturer had given me however it still results in an error. The error is found on an application.html.erb page (which basically holds the links to other pages on the website, they are on the home page) in the layouts of notepad++. This may be a very vague question but maybe someone with a better understanding of ruby might point me in the right direction.

<% if !signed_in? %> **(This line is highlighted red)**
       <li><%=link_to "Sign In", login_path %></li>
        <% else %>
       <li>Signed in as <%= @current_candidate.name%></li>
       <li><%= link_to "Sign Out", logout_path%></li>

 app/helpers/application_helper.rb:3:in `signed_in?'
 app/views/layouts/application.html.erb:29:in `_app_views_layouts_application_html_erb___772860813_51372912'

* Application Helper *

   module ApplicationHelper   
   def signed_in?
   if session[:candidate_id].nil  
    return  
   else      
   @current_candidate = Candidate.find_by_id(session[:candidate_id]) 
  end
  end
 end

Solution

  • Assuming it's not a typo here

    if session[:candidate_id].nil  
      return  
    else      
     @current_candidate = Candidate.find_by_id(session[:candidate_id]) 
    end
    

    You need a question mark on the end of nil

    if session[:candidate_id].nil?
      return  
    else      
      @current_candidate = Candidate.find_by_id(session[:candidate_id]) 
    end