Rails named route not recognized - ruby-on-rails

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

Related

Simple routing issue - Signup, login, logout, "no route matchs GET '/Signup'

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.

Routing Error in Chapter 7.1.2 of the Ruby on Rails Tutorial

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.

error when routing

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.

Rails routes error

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

undefined local variable or method `summitted_password' Ruby on Rails Tutorial Chapter 9

I'm reading Ruby on Rails tutorial. On chapter 9 after wrote all the code, for sign in and remember me code. My rspec test still fail. this are the exactly error:
Failures:
1) SessionsController POST 'create' with valid email and password should sign the user in
Failure/Error: post :create, :session => #attr
undefined local variable or method `summitted_password' for #<Class:0x007fc05395e280>
# ./app/models/user.rb:38:in `authenticate'
# ./app/controllers/sessions_controller.rb:7:in `create'
# ./spec/controllers/sessions_controller_spec.rb:47:in `block (4 levels) in <top (required)>'
2) SessionsController POST 'create' with valid email and password should redirect to the user show page
Failure/Error: post :create, :session => #attr
undefined local variable or method `summitted_password' for #<Class:0x007fc05395e280>
# ./app/models/user.rb:38:in `authenticate'
# ./app/controllers/sessions_controller.rb:7:in `create'
# ./spec/controllers/sessions_controller_spec.rb:53:in `block (4 levels) in <top (required)>'
Finished in 0.90025 seconds
7 examples, 2 failures
Does anybody have the same problem, because I didn't find any typo.
This is my session_controller
class SessionsController < ApplicationController
def new
#title = "Sign in"
end
def create
user = User.authenticate(params[:session][:email],
params[:session][:password])
if user.nil?
flash.now[:error] = "Invalid email/password combination."
#title = "Sign in"
render 'new'
else
sign_in user
redirect_to user
end
end
def destroy
end
end
After fixed the type on the user model, I get this error on the test:
Failures:
1) SessionsController POST 'create' with valid email and password should sign the user in
Failure/Error: post :create, :session => #attr
undefined method `user_url' for #<SessionsController:0x007fc347354e38>
# ./app/controllers/sessions_controller.rb:15:in `create'
# ./spec/controllers/sessions_controller_spec.rb:47:in `block (4 levels) in <top (required)>'
2) SessionsController POST 'create' with valid email and password should redirect to the user show page
Failure/Error: post :create, :session => #attr
undefined method `user_url' for #<SessionsController:0x007fc34679aa20>
# ./app/controllers/sessions_controller.rb:15:in `create'
# ./spec/controllers/sessions_controller_spec.rb:53:in `block (4 levels) in <top (required)>'
Finished in 0.58335 seconds
This is my bundle exec rake routes
WARNING: 'require 'rake/rdoctask'' is deprecated. Please use 'require 'rdoc/task' (in RDoc 2.4.2+)' instead.
at /Users/jeanosorio/.rvm/gems/ruby-1.9.2-p290#rails3tutorial/gems/rake-0.9.2.2/lib/rake/rdoctask.rb
WARNING: Global access to Rake DSL methods is deprecated. Please include
... Rake::DSL into classes and modules which use the Rake DSL methods.
WARNING: DSL method SampleApp::Application#task called at /Users/jeanosorio/.rvm/gems/ruby-1.9.2-p290#rails3tutorial/gems/railties-3.0.0/lib/rails/application.rb:214:in `initialize_tasks'
users POST /users(.:format) {:action=>"create", :controller=>"users"}
new_users GET /users/new(.:format) {:action=>"new", :controller=>"users"}
edit_users GET /users/edit(.:format) {:action=>"edit", :controller=>"users"}
users GET /users(.:format) {:action=>"show", :controller=>"users"}
users PUT /users(.:format) {:action=>"update", :controller=>"users"}
users DELETE /users(.:format) {:action=>"destroy", :controller=>"users"}
sessions POST /sessions(.:format) {:action=>"create", :controller=>"sessions"}
new_sessions GET /sessions/new(.:format) {:action=>"new", :controller=>"sessions"}
sessions DELETE /sessions(.:format) {:action=>"destroy", :controller=>"sessions"}
signup /signup(.:format) {:controller=>"users", :action=>"new"}
signin /signin(.:format) {:controller=>"sessions", :action=>"new"}
signout /signout(.:format) {:controller=>"sessions", :action=>"destroy"}
contact /contact(.:format) {:controller=>"pages", :action=>"contact"}
about /about(.:format) {:controller=>"pages", :action=>"about"}
help /help(.:format) {:controller=>"pages", :action=>"help"}
root /(.:format) {:controller=>"pages", :action=>"home"}
routes.rb file
SampleApp::Application.routes.draw do
resource :users
resource :sessions, :only => [:new, :create, :destroy]
match '/signup', :to => 'users#new'
match '/signin', :to => 'sessions#new'
match '/signout', :to => 'sessions#destroy'
match '/contact', :to => 'pages#contact'
match '/about', :to => 'pages#about'
match '/help', :to => 'pages#help'
root :to => 'pages#home'
end
Thanks
You are a letter out.
it should be submitted_password not summitted_password on line 38 of user.rb

Resources