I am studying Ruby on rails by Michael Hartl. I am stuck at the section 8.1.4 which is implementing a sign-in page using Rails 3.2.3 with Ruby 1.9.3-p125.
I have created a session controller, and I want my session controller’s create action maps to this route /sessions , but always a routing error. Any clues? The following are my relevant files:
routes.rb
SampleApp::Application.routes.draw do
resources :users
resources :sessions, only: [:new, :create, :destroy]
root to: 'static_pages#home'
match '/signup', to: 'users#new'
match '/signin', to: 'sessions#new'
match '/signout', to: 'sessions#destroy', via: :delete
match '/help', to: 'static_pages#help'
match '/about', to: 'static_pages#about'
match '/contact', to: 'static_pages#contact'
end
and my rake routes :
users GET /users(.:format) users#index
POST /users(.:format) users#create
new_user GET /users/new(.:format) users#new
edit_user GET /users/:id/edit(.:format) users#edit
user GET /users/:id(.:format) users#show
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy
sessions POST /sessions(.:format) sessions#create
new_session GET /sessions/new(.:format) sessions#new
session DELETE /sessions/:id(.:format) sessions#destroy
root / static_pages#home
signup /signup(.:format) users#new
signin /signin(.:format) sessions#new
signout DELETE /signout(.:format) sessions#destroy
help /help(.:format) static_pages#help
about /about(.:format) static_pages#about
contact /contact(.:format) static_pages#contact
my app/views/sessions/new.html.erb :
<%= provide(:title, 'Sign in') %>
<h1>Sign in</h1>
<div class="row">
<div class="span6 offset3">
<%= form_for(:session, url: sessions_path) do |f| %>
<%= f.label :email %>
<%= f.text_field :email %>
<%= f.label :password %>
<%= f.password_field :password %>
<%= f.submit "Sign in", class: "btn btn-large btn-primary" %>
<% end %>
<p>New user? <%= link_to "Sign up now!", signup_path %></p>
</div>
my sessions controller:
class SessionsController < ApplicationController
def new
end
def create
render 'new'
end
def destroy
end
end
In figure 8.5 the picture shows the url for sign in is http://localhost:3000/sessions. When I navigate to that page,
I always get a
Routing Error
No route matches [GET] "/sessions"
solved
The figure 8.5 is showing the page after you hit the create button, and the url changes from /sessions/new or /signin to /sessions, So it’s nothing wrong with my above files.
I think it’s a typo. You have to naviagte to
http://localhost:3000/sessions/newas indicated in your routes.