Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby

Shawn Wilson
seal-mask
.a{fill-rule:evenodd;}techdegree
Shawn Wilson
iOS Development Techdegree Student 7,049 Points

After trying to login unsuccessfully my app gives me "No route matches [POST] "/user_session/new" " error

So one last hickup for the night then i'm calling it quits.. when i go to log in to my page weather it is successful or not it throws me the error in the title of the post:

" No route matches [POST] "/user_session/new" "

here is my routes.rb file

Rails.application.routes.draw do

    resources :users
    resources :user_session, only: [:new, :create]

    root 'user_session#new'
end

here is the user_session new.htnl.erb file

<h1>Log Into S.I.C.</h1>

<%= form_tag @user_session_path do %>
  <div class="login_boxes">
    Badge Number:<%= text_field_tag :badge_number, params[:badge_number] %>
    </br>
    </br>
    Password: <%= password_field_tag :password %>
  </div>
    </br>
    </br>
  <div class="action">
    <%= submit_tag "Log In" %>
  </div>
<% end %>

here is the user_session_controller

class UserSessionController < ApplicationController

  def new

  end

  def create
    user = User.find_by(badge_number: params[:badge_number])
    if user && user.authenticate(params[:password])
      session[:user_id] = @user.id
      flash[:success] = "Welcome to Security Information Center"
      redirect_to users_path
    else
      flash[:error] = "Could not log you into S.I.C. Please check your credentials and try again."
      render action: 'new'
    end
  end

end

6 Answers

Maciej Czuchnowski
Maciej Czuchnowski
36,441 Points

OK, so you have to modify your sessions create method, these three lines. They have to all contain either local OR instance variables, not both:

    @user = User.find_by(badge_number: params[:badge_number])
    if @user && @user.authenticate(params[:password])
      session[:user_id] = @user.id

or

    user = User.find_by(badge_number: params[:badge_number])
    if user && user.authenticate(params[:password])
      session[:user_id] = user.id

I created a new user in the console and successfully logged him in after these changes.

Maciej Czuchnowski
Maciej Czuchnowski
36,441 Points

This seems odd:

<%= form_tag @user_session_path do %>

It looks like a crossover of a user instance variable and a route helper.

Shawn Wilson
seal-mask
.a{fill-rule:evenodd;}techdegree
Shawn Wilson
iOS Development Techdegree Student 7,049 Points

so it actually errors out harder when i remove the @ sign... i looked at the link above but still cant figure this out..

this is that it gives me as routes

Helper HTTP Verb Path Controller#Action Path / Url
users_path GET /users(.:format) users#index POST /users(.:format) users#create new_user_path GET /users/new(.:format) users#new edit_user_path GET /users/:id/edit(.:format) users#edit user_path GET /users/:id(.:format) users#show PATCH /users/:id(.:format) users#update PUT /users/:id(.:format) users#update DELETE /users/:id(.:format) users#destroy user_session_index_path POST /user_session(.:format) user_session#create new_user_session_path GET /user_session/new(.:format) user_session#new root_path GET / user_session#new

Shawn Wilson
seal-mask
.a{fill-rule:evenodd;}techdegree
Shawn Wilson
iOS Development Techdegree Student 7,049 Points

GOT THE LITTLE BUGGER... thanks i had in my user_session_controller.rb the following:

def create user = User.find_by(badge_number: params[:badge_number]) if user && user.authenticate(params[:password]) session[:user_id] = @user.id flash[:success] = "Welcome to Security Information Center" redirect_to users_path else flash[:error] = "Could not log you into S.I.C. Please check your credentials and try again." render action: 'new' end end

i removed the @ symbol on line 4 and it passed the login in and took me to the page it needed to!

the only thing i haven't learned as of yet is how to log out of the session and redirect back to the log in screen it dosnt seem to go over that

Maciej Czuchnowski
Maciej Czuchnowski
36,441 Points

Just create a destroy action (and route) which changes the session[:user_id] to nil.