Rails authentication - step 11/12

All steps are complete and checked off but app has error message:

Hi Lenka,

Could you please copy/paste in your app/views/application.html.erb and app/models/application.rb code?

Thanks!


I think maybe you meant app/controllers/application_controller.rb for the second one. There is no “app/models/application.rb”…

<!DOCTYPE html>

<html>
  <head>
    <link href="http://s3.amazonaws.com/codecademy-content/projects/bootstrap.min.css" rel="stylesheet" />
    <link href='http://fonts.googleapis.com/css?family=Roboto:400,500,300' rel='stylesheet' type='text/css'>
    
    <%= stylesheet_link_tag    'application', media: 'all', 'data-turbolinks-track' => true %>
    <%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
    <%= csrf_meta_tags %>
  </head>
  <body>

    <div class="header">
      <div class="container">
        <%= link_to root_path do %>
          <img src="http://s3.amazonaws.com/codecademy-content/courses/rails-auth/img/fotofoto-logo.svg" width="80" height="80"> &#12501; &#65387; &#12488; &#12501; &#65387; &#12488;
        <% end %>

        <div class="nav pull-right">
          <% if current_user %> 
  <ul> 
    <li><%= current_user.email %></li> 
    <li><%= link_to "Log out", logout_path, method: "delete" %></li> 
  </ul> 
<% else %> 
  <ul> 
    <li><%= link_to "Login", 'login' %></a></li> 
    <li><%= link_to "Signup", 'signup' %></a></li> 
  </ul> 
<% end %>
            
          
        </div>
      </div>
    </div>

    <%= yield %>

  </body>
</html>
class ApplicationController < ActionController::Base
  
  def current_user
    @current_user ||= User.find(session[:user_id]) if session[:user_id]
  end
  
  def require_user
    redirect_to '/login' unless current_user
  end
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  protect_from_forgery with: :exception
end
1 Like

@lenkie Good job formatting your code - just remember to put the backticks (`) on their own line before and after the code :slight_smile:

And you’re correct about the file, thanks for pointing that out. Try adding this line to line 2 of application_controller.rb:

helper_method :current_user

OK thanks. That helped bring up the login screen but when I log in, it says “Not found”…

@lenkie Could you post a screenshot?

Also, if I could see your config/routes.rb, I think that’s where the problem is :slight_smile:

Rails.application.routes.draw do
  root 'albums#index'
  get 'albums' => 'albums#index'
  get 'albums/new' => 'albums#new'
  get 'albums/:id' => 'albums#show', as: :album
  post 'albums' => 'albums#create'
  
  get 'signup'  => 'users#new' 
  resources :users
  
  get '/login' => 'sessions#new'
  post 'login' => 'sessions#create'
  
  delete 'logout' => 'sessions#destroy'
 
end

@lenkie Hm, so far, all of your code looks fine :sweat:

If refreshing your browser doesn’t change anything, could you post your sessions_controller.rb code?

class SessionsController < ApplicationController
  def new
  end
  
  def create
  @user = User.find_by_email(params[:session][:email])
  if @user && @user.authenticate(params[:session][:password])
    session[:user_id] = @user.id
    redirect_to '/'
  else
    redirect_to 'login'
  end 
end
  
  def destroy
    session[:user_id] = nil
    redirect_to '/'
  end
  
end

@lenkie I’m not finding any problems in your code, so I’m not sure what’s wrong :confused:

You did create an account in the previous exercise (I think exercise 4), right? I don’t think not having an account would cause an error, but I’m not sure what else the problem could be.

OK, I had already created an account but I went back and did it again, just to see if anything changed, and it did. So then I was able to sign in in step 11. Thanks. I guess there’s some bug in the program or something. Anyway, moving on :slight_smile:

2 Likes

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.