I am following along with Michael Hartl's Ruby on Rails Tutorial 2nd Edition and have reached the signin/signout section of the book.
So far I can create a new user (or in my case landlord) and log in with the new credentials. The problem I have is when signing out. I click "signout" and get a route error saying:
No route matches [GET] "/signout"
Below are code snippets. Any help would be very appreciated!
rake routes output
landlords GET /landlords(.:format) landlords#index
POST /landlords(.:format) landlords#create
new_landlord GET /landlords/new(.:format) landlords#new
edit_landlord GET /landlords/:id/edit(.:format) landlords#edit
landlord GET /landlords/:id(.:format) landlords#show
PUT /landlords/:id(.:format) landlords#update
DELETE /landlords/:id(.:format) landlords#destroy
properties GET /properties(.:format) properties#index
POST /properties(.:format) properties#create
new_property GET /properties/new(.:format) properties#new
edit_property GET /properties/:id/edit(.:format) properties#edit
property GET /properties/:id(.:format) properties#show
PUT /properties/:id(.:format) properties#update
DELETE /properties/:id(.:format) properties#destroy
sessions POST /sessions(.:format) sessions#create
new_session GET /sessions/new(.:format) sessions#new
session DELETE /sessions/:id(.:format) sessions#destroy
root / content_pages#home
content_pages_home GET /content_pages/home(.:format) content_pages#home
help /help(.:format) content_pages#help
questions /questions(.:format) content_pages#questions
signup /signup(.:format) landlords#new
signin /signin(.:format) sessions#new
signout DELETE /signout(.:format) sessions#destroy
routes.rb file
resources :landlords
resources :properties
resources :sessions, only: [:new, :create, :destroy]
root :to => 'content_pages#home'
get "content_pages/home"
match '/help', to: 'content_pages#help'
match '/questions', to: 'content_pages#questions'
match '/signup', to: 'landlords#new'
match '/signin', to: 'sessions#new'
match '/signout', to: 'sessions#destroy', via: :delete
link to signout
<%= link_to "Signout", signout_path, method: "delete" %>
sessions controller
def destroy
sign_out
redirect_to root_path
end
The via: option in the following code restricts the request to the delete method:
match '/signout', to: 'sessions#destroy', via: :delete
You'll need to make one that works with the 'get' method
Check out the Rails Routing guide
Related
I have setup a Signup, Login, Logout with bcrypt. I have set up basic routing as shown below however keep getting the same error with
"no route matches GET '/Signup'"...
anyone, please help I'm confused - '/Signup' should go to sessions#new which is new.html.erb in sessions right? Please clarify... There might also be something wrong with my controllers which I will post if necessary as well.
Thanks for any help.
Rails.application.routes.draw do
resources :users, controller: :sessions
root 'users#index'
get '/signup', to: 'users#new'
get '/signup', to: 'users#update'
post '/signup', to: 'users#create'
get '/login', to: 'sessions#new'
post '/login', to: 'sessions#create'
get '/logout', to: 'sessions#destroy'
end
Look, if you use resources :users then you don't need to declare again these lines on the routes.rb file
get '/signup', to: 'users#new'
get '/signup', to: 'users#update'
post '/signup', to: 'users#create'
because resources :users providing all of these like after
resources :users
then 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
PATCH /users/:id(.:format) users#update
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy
this new_user_path for Signup form and users_path post method is user create route, so for signup you have to use users controller.
Same as for sessions and login after use resources :sessions instead of three lines yours, you can get after rake routes
sessions GET /sessions(.:format) sessions#index
POST /sessions(.:format) sessions#create
new_session GET /sessions/new(.:format) sessions#new
edit_session GET /sessions/:id/edit(.:format) sessions#edit
session GET /sessions/:id(.:format) sessions#show
PATCH /sessions/:id(.:format) sessions#update
PUT /sessions/:id(.:format) sessions#update
DELETE /sessions/:id(.:format) sessions#destroy
For login you can use sessions controller, for sessions only create and destroy you can more specific your sessions route like just edit sessions route
resources :sessions, only: [:new, :create, :destroy]
you can get now only three routes for sessions like new,create&destroy
sessions POST /sessions(.:format) sessions#create
new_session GET /sessions/new(.:format) sessions#new
session DELETE /sessions/:id(.:format) sessions#destroy
For Signup form users/new.html.erb and for Login form sessions/new.html.erb
You can follow the Michael Hartl tutorial for basic authentication understand.
To create a singular resource, you must specify:
HTTP Verb - Path - Controller#Action
e.g. get '/signup', to: 'controller#action'
You need know what is the controller and action responsible for create the new user and then replace controller#action.
For more details about routes look rails routing.
I have started working on Chapter 8 of the famous Rails tutorial. I think I followed the instructions closely and defined the following routes:
SampleApp::Application.routes.draw do
resources :users
resources :sessions, only: [:new, :create, :destroy]
root 'static_pages#home'
match '/signup', to: 'users#new', via: 'get'
match '/signin', to: 'sessions#new', via: 'get'
match '/signout', to: 'sessions#destroy', via: 'delete'
match '/help', to: 'static_pages#help', via: 'get'
match '/about', to: 'static_pages#about', via: 'get'
match '/contact', to: 'static_pages#contact', via: 'get'
The session controller (/controllers/sessions_controller.rb) is defined as follows:
class SessionsController < ApplicationController
def new
end
def create
end
def destroy
end
end
In spec/requests/authentication_pages_spec.rb I have created the following test:
require 'spec_helper'
describe "Authentication" do
subject {page}
describe "signin page" do
before { visit signin_path}
it { should have_content('Sign in')}
it { should have_title('Sign in')}
end
end
The test causes the following errors:
Failures:
1) Authentication signin page
Failure/Error: before { visit signin_path}
NameError:
undefined local variable or method `signin_path' for #
<RSpec::Core::ExampleGroup::Nested_2::Nested_1:0x007f98dec05fe8>
# ./spec/requests/authentication_pages_spec.rb:7:in `block (3 levels) in <top (required)>'
2) Authentication signin page
Failure/Error: before { visit signin_path}
NameError:
undefined local variable or method `signin_path' for #
<RSpec::Core::ExampleGroup::Nested_2::Nested_1:0x007f98dec5cf50>
# ./spec/requests/authentication_pages_spec.rb:7:in `block (3 levels) in <top (required)>'
It seems the signin_path named route is not recognised even though it is defined in routes.rb. I replaced that named route with one of the others (signup_path) and the problem disappeared. So, it is something about this particular named route. Can you tell what the problem is?
rake routes produces the following output:
sb7904313:sample_app nnikolo$ rake routes
Prefix Verb URI Pattern Controller#Action
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
PATCH /users/:id(.:format) users#update
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 GET / static_pages#home
signup GET /signup(.:format) users#new
signin GET /signin(.:format) sessions#new
signout DELETE /signout(.:format) sessions#destroy
help GET /help(.:format) static_pages#help
about GET /about(.:format) static_pages#about
contact GET /contact(.:format) static_pages#contact
I also re-started the server and it did not solve the problem (I made a post to the contrary but I was wrong).
Try this instead. You can access your route names by the command rake routes. Since it's coming from your sessions controller, by default the route is probably something like new_session_path. To change it, you need to specify what you what to change it to in your routes with as: new_name
match '/signin', to: 'sessions#new', via: 'get', as: 'signin'
Try:--
get "signin" => "sessions#new", :as => "signin"
resources :sessions
Use the route as new_session_path or signin_path
See this thread: undefined method `visit' when using RSpec and Capybara in rails
You possibly did not have the line config.include Capybara:DSL in your spec_helper.rb and something about putting the tests inside rspec/features/ due to the latest changes in Capybara.
I was wrong about the re-start solving the problem so retract this post.
==EDIT I found the culprit for this erratic behaviour of rspec: rails does not seem to empty the cache between tests (which is, in my opinion, a scary bug). By default it fails to re-read the files and thus may ignore changes that have occurred. I put more details here: Rails tutorial: Rails tutorial: undefined method
I've been working through the tutorial for the past few days, and finally hit a snag in chapter 7.
It is in this step where the line in routes.rb:
get "users/new"
is replaced with
resource :users
After I do this, I get a routing error when visiting
http://localhost:3000/users/1 - No route matches [GET] "/users/1"
instead of the other "Unknown Action" error shown here.
Per the instructions, my routes.db file looks like this:
SampleApp::Application.routes.draw do
resource :users
root "static_pages#home"
match '/signup', to: 'users#new', via: 'get'
match '/help', to: 'static_pages#help', via: 'get'
match '/about', to: 'static_pages#about', via: 'get'
match '/contact', to: 'static_pages#contact', via: 'get'
end
Output from 'rake routes' shows:
Prefix Verb URI Pattern Controller#Action
users POST /users(.:format) users#create
new_users GET /users/new(.:format) users#new
edit_users GET /users/edit(.:format) users#edit
GET /users(.:format) users#show
PATCH /users(.:format) users#update
PUT /users(.:format) users#update
DELETE /users(.:format) users#destroy
root GET / static_pages#home
signup GET /signup(.:format) users#new
help GET /help(.:format) static_pages#help
about GET /about(.:format) static_pages#about
contact GET /contact(.:format) static_pages#contact
Does anyone have any insight to get past this? Many thanks.
I think you should use resources :users.
Singular routes are best suited when you have one and only resource to deal with. I.E. resource :profile because one user has one profile.
I have routes defined to some static pages and also to some controllers (users and usymptoms).
When I navigate to localhost:3000/users/1 or localhost:3000/users/1/usymptoms/new everything is fine. Once I finish filling in the form on symptoms/new, I have the usymptoms controller redirecting to #users. This works fine.
In the model file, the association is users has many usymptoms and usymptoms belongs to user.
However, now my static pages are not accessible. For example, when I navigate to /learn, I get the following error:
No route matches {:action=>"new", :controller=>"usymptoms", :user_id=>nil}
I am new to rails. Can you please help me figure out the error?
I have provided my routes file and the output from "rake routes" below.
My routes.rb file
root to: 'static_pages#home'
match '/about', to: 'static_pages#about'
match '/contact', to: 'static_pages#contact'
match '/learn', to: 'static_pages#learn'
match '/signup', to: 'users#new'
match '/signin', to: 'sessions#new'
match '/signout', to: 'sessions#destroy', via: :delete
resources :users do
resources :usymptoms
end
resources :sessions, only: [:new, :create, :destroy]
======
Output from rake routes
root / static_pages#home
about /about(.:format) static_pages#about
contact /contact(.:format) static_pages#contact
learn /learn(.:format) static_pages#learn
signup /signup(.:format) users#new
signin /signin(.:format) sessions#new
signout DELETE /signout(.:format) sessions#destroy
user_usymptoms GET /users/:user_id/usymptoms(.:format) usymptoms#index
POST /users/:user_id/usymptoms(.:format) usymptoms#create
new_user_usymptom GET /users/:user_id/usymptoms/new(.:format) usymptoms#new
edit_user_usymptom GET /users/:user_id/usymptoms/:id/edit(.:format) usymptoms#edit
user_usymptom GET /users/:user_id/usymptoms/:id(.:format) usymptoms#show
PUT /users/:user_id/usymptoms/:id(.:format) usymptoms#update
DELETE /users/:user_id/usymptoms/:id(.:format) usymptoms#destroy
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
You must have the following link somewhere: new_user_usymptom_path(#user), but #user is nil for some reason, hence the error.
I am trying to track down a particularly elusive bug in working through Michael Hartl's ROR Tutorial.
When clicking on 'Delete' for a micropost (from the home page or the user/show page) the url is http://localhost:3000/microposts/303, but the result is "Routing Error - No route matches"/microposts/303".
I have gone through each page of my code that is involved and replaced them with code from Hartl's gitHub project site. https://github.com/railstutorial/sample_app. For example, for the microposts_controller, I copied the code from the gitHub depot and replaced my code with the copied code. I then restarted the server. Same result. I then reverted back to my code to test the next page.
The pages I swapped the code with are
CONTROLLERS
microposts_controller
users_controller (show method)
MODEL
micropost.rb (model)
VIEWS
microposts/_micropost.haml
shared/_micropost_form.html.haml
shared/_feed.haml
shared/_feed_item.haml
and the Routes file.
I am at a loss for other things to check. Does anyone have any suggestions?
Thanks,
Dave
The results of rake routes
sessions POST /sessions(.:format) {:action=>"create", :controller=>"sessions"}
new_session GET /sessions/new(.:format) {:action=>"new", :controller=>"sessions"}
session DELETE /sessions/:id(.:format) {:action=>"destroy", :controller=>"sessions"}
signin /signin(.:format) {:controller=>"sessions", :action=>"new"}
signout /signout(.:format) {:controller=>"sessions", :action=>"destroy"}
microposts POST /microposts(.:format) {:action=>"create", :controller=>"microposts"}
micropost DELETE /microposts/:id(.:format) {:action=>"destroy", :controller=>"microposts"}
root /(.:format) {:controller=>"pages", :action=>"home"}
contact /contact(.:format) {:controller=>"pages", :action=>"contact"}
about /about(.:format) {:controller=>"pages", :action=>"about"}
help /help(.:format) {:controller=>"pages", :action=>"help"}
signup /signup(.:format) {:controller=>"users", :action=>"new"}
development /development(.:format) {:controller=>"pages", :action=>"development"}
/signup(.:format) {:controller=>"users", :action=>"new"}
users GET /users(.:format) {:action=>"index", :controller=>"users"}
POST /users(.:format) {:action=>"create", :controller=>"users"}
new_user GET /users/new(.:format) {:action=>"new", :controller=>"users"}
edit_user GET /users/:id/edit(.:format) {:action=>"edit", :controller=>"users"}
user GET /users/:id(.:format) {:action=>"show", :controller=>"users"}
PUT /users/:id(.:format) {:action=>"update", :controller=>"users"}
DELETE /users/:id(.:format) {:action=>"destroy", :controller=>"users"}
The Routes.rb file is
SampleApp::Application.routes.draw do
#Sign in Routes
resources :sessions, :only => [:new, :create, :destroy]
match '/signin', :to => 'sessions#new'
match '/signout', :to => 'sessions#destroy'
#Microposts Routes
resources :microposts, :only => [:create, :destroy]
#Pages Routes
root :to => "pages#home"
match '/contact', :to => 'pages#contact'
match '/about', :to => 'pages#about'
match '/help', :to => 'pages#help'
match '/signup', :to => 'users#new'
match '/development', :to => 'pages#development'
#Users Routes
match '/signup', :to => 'users#new'
resources :users
end
But, as I said, even replacing my routes file with the one on gitHub did not resolve the issue.
The link to delete is
= link_to "delete", micropost, :method => :delete,
:confirm => "You sure?",
:title => micropost.content
link_to :method => :delete uses unobtrusive javascript to create the DELETE request. My guess is that you either don't have the necessary javascript files in your project (prototype.js/jquery.js and rails.js) or you are not including them in your layout.