For my test I have the following:
test "should update holder" do
holder = Holder.create(name: "name", user_id: 10)
put :update, holder: holder
assert_redirected_to holder_path(assigns(:holder))
end
And when I run them I get the following Error:
ERROR (0:00:00.185) test_should_update_holder
No route matches {:holder=>"980190963", :controller=>"holders", :action=>"update"}
# /usr/local/lib/ruby/gems/1.9.1/gems/actionpack-3.2.3/lib/action_dispatch/routing/route_set.rb:532:in `raise_routing_error'
/usr/local/lib/ruby/gems/1.9.1/gems/actionpack-3.2.3/lib/action_dispatch/routing/route_set.rb:528:in `rescue in generate'
/usr/local/lib/ruby/gems/1.9.1/gems/actionpack-3.2.3/lib/action_dispatch/routing/route_set.rb:520:in `generate'
/usr/local/lib/ruby/gems/1.9.1/gems/actionpack-3.2.3/lib/action_dispatch/routing/route_set.rb:561:in `generate'
/usr/local/lib/ruby/gems/1.9.1/gems/actionpack-3.2.3/lib/action_dispatch/routing/route_set.rb:557:in `generate_extras'
/usr/local/lib/ruby/gems/1.9.1/gems/actionpack-3.2.3/lib/action_dispatch/routing/route_set.rb:553:in `extra_keys'
/usr/local/lib/ruby/gems/1.9.1/gems/actionpack-3.2.3/lib/action_controller/test_case.rb:147:in `assign_parameters'
/usr/local/lib/ruby/gems/1.9.1/gems/actionpack-3.2.3/lib/action_controller/test_case.rb:453:in `process'
/usr/local/lib/ruby/gems/1.9.1/gems/actionpack-3.2.3/lib/action_controller/test_case.rb:49:in `process'
/usr/local/lib/ruby/gems/1.9.1/gems/actionpack-3.2.3/lib/action_controller/test_case.rb:390:in `put'
test/functional/holders_controller_test.rb:36:in `block in <class:HoldersControllerTest>'
/usr/local/lib/ruby/gems/1.9.1/gems/activesupport-3.2.3/lib/active_support/testing/setup_and_teardown.rb:35:in `block in run'
/usr/local/lib/ruby/gems/1.9.1/gems/activesupport-3.2.3/lib/active_support/callbacks.rb:458:in `_run__4148286245602197272__setup__4285546581512185515__callbacks'
/usr/local/lib/ruby/gems/1.9.1/gems/activesupport-3.2.3/lib/active_support/callbacks.rb:405:in `__run_callback'
/usr/local/lib/ruby/gems/1.9.1/gems/activesupport-3.2.3/lib/active_support/callbacks.rb:385:in `_run_setup_callbacks'
/usr/local/lib/ruby/gems/1.9.1/gems/activesupport-3.2.3/lib/active_support/callbacks.rb:81:in `run_callbacks'
/usr/local/lib/ruby/gems/1.9.1/gems/activesupport-3.2.3/lib/active_support/testing/setup_and_teardown.rb:34:in `run'
But in my routes I have:
Teacherjoy::Application.routes.draw do
get "users/new"
resources :questions
resources :pages
resources :holders
resources :users
resources :sessions, only: [:new, :create, :destroy]
root :to => 'pages#home'
match '/signup', to: 'users#new'
match '/signin', to: 'sessions#new'
match '/signout', to: 'sessions#destroy', via: :delete
end
and rake routes returns:
[teacherjoy (master)]$ rake routes
users_new GET /users/new(.:format) users#new
questions GET /questions(.:format) questions#index
POST /questions(.:format) questions#create
new_question GET /questions/new(.:format) questions#new
edit_question GET /questions/:id/edit(.:format) questions#edit
question GET /questions/:id(.:format) questions#show
PUT /questions/:id(.:format) questions#update
DELETE /questions/:id(.:format) questions#destroy
pages GET /pages(.:format) pages#index
POST /pages(.:format) pages#create
new_page GET /pages/new(.:format) pages#new
edit_page GET /pages/:id/edit(.:format) pages#edit
page GET /pages/:id(.:format) pages#show
PUT /pages/:id(.:format) pages#update
DELETE /pages/:id(.:format) pages#destroy
holders GET /holders(.:format) holders#index
POST /holders(.:format) holders#create
new_holder GET /holders/new(.:format) holders#new
edit_holder GET /holders/:id/edit(.:format) holders#edit
holder GET /holders/:id(.:format) holders#show
PUT /holders/:id(.:format) holders#update
DELETE /holders/:id(.:format) holders#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
root / pages#home
signup /signup(.:format) users#new
signin /signin(.:format) sessions#new
signout DELETE /signout(.:format) sessions#destroy
If you look at rake routes, there clearly is a an action for update, which is a put, in the holders controller, which is what my test is doing, right?
Notice that the route is actually PUT /holders/:id, but you're passing a :holder option to your put method, not an :id. Try changing that line in your test to this:
put :update, id: holder
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'm a rails beginner struggling with routes a bit. I'm working on a Q&A site (or question and response) and any time I try to post a response, it gives the following error:
No route matches [GET] "/questions/6/responses"
Any help would be greatly appreciated,
Jon
Here are the relevant bits:
Routes output (given w the error)
(apologies for nasty paste):
Helper HTTP Verb Path Controller#Action
Path / Url
welcome_index_path GET /welcome/index(.:format) welcome#index
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
categories_path GET /categories(.:format) categories#index
POST /categories(.:format) categories#create
new_category_path GET /categories/new(.:format) categories#new
edit_category_path GET /categories/:id/edit(.:format) categories#edit
category_path GET /categories/:id(.:format) categories#show
PATCH /categories/:id(.:format) categories#update
PUT /categories/:id(.:format) categories#update
DELETE /categories/:id(.:format) categories#destroy
question_responses_path POST /questions/:question_id/responses(.:format) responses#create
questions_path GET /questions(.:format) questions#index
POST /questions(.:format) questions#create
new_question_path GET /questions/new(.:format) questions#new
edit_question_path GET /questions/:id/edit(.:format) questions#edit
question_path GET /questions/:id(.:format) questions#show
PATCH /questions/:id(.:format) questions#update
PUT /questions/:id(.:format) questions#update
DELETE /questions/:id(.:format) questions#destroy
root_path GET / welcome#index
Routes:
Rails.application.routes.draw do
get 'welcome/index'
resources :users
resources :categories
resources :questions do
resources :responses, :only => [:create]
end
root 'welcome#index'
Questions controller
https://github.com/joncarpe/snack/blob/master/app/controllers/questions_controller.rb
Responses controller
https://github.com/joncarpe/snack/blob/master/app/controllers/responses_controller.rb
You have only route to create action in ResponsesController. If you also want route to index, you should have:
resources :responses, only: [:create, :index]
If you want routes to all default resources actions, you should abandon only option, like this:
resources :responses
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 doing Michael Hartls Ruby on Rails Tutorial and I am getting a routing error when I try to browse to localhost:3000/sessions
Routing Error
No route matches "/sessions"
From the tutorial, I was under the impression that rails will infer the route to "sessions" and I would not need to add a specification route to routes.rb.
If I run rake routes I get the following
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"}
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"}
root /(.:format) {:controller=>"pages", :action=>"home"}
signup /signup(.:format) {:controller=>"users", :action=>"new"}
signin /signin(.:format) {:controller=>"sessions", :action=>"new"}
signout /signout(.:format) {:controller=>"sessions", :action=>"destroy"}
about /about(.:format) {:controller=>"pages", :action=>"about"}
contact /contact(.:format) {:controller=>"pages", :action=>"contact"}
help /help(.:format) {:controller=>"pages", :action=>"help"}
My routes.rb contains
SampleApp::Application.routes.draw do
resources :users
resources :sessions, :only => [:new, :create, :destroy]
root :to => 'pages#home'
match '/signup', :to => 'users#new'
match '/signin', :to => 'sessions#new'
match '/signout', :to => 'sessions#destroy'
match '/about', :to => 'pages#about'
match '/contact', :to => 'pages#contact'
match '/help', :to => 'pages#help'
I can get it to work if I add the following line to routes.rb, but I didn't think I needed to do this explicitly
match '/sessions',:to => 'sessions#create'
Am I missing something or misunderstanding something?
I am running Rails 3.0.11 and Ruby 1.9.2p290
In rails a GET verb request to /sessions routes to the index action.
You either need to browse to /sessions/new, or add the additional match like you've done in the last part of your question.
These are the default routes for a resource: http://guides.rubyonrails.org/routing.html#crud-verbs-and-actions
I have the following going on:
rspec test in users_controller_spec:
it "should redirect to the user show page" do
post :create, :user => #attr
response.should redirect_to(user_path(assigns(:user)))
end
In my users_controller I have the following:
def show
#user = User.find(params[:id])
#title = #user.name
end
def create
#title = "Sign up"
#user = User.new(params[:user])
if #user.save
redirect_to #user, :notice => "Signed Up!"
else
#title = "Sign up"
render "new"
end
end
In my routes.rb I have the following:
Psra::Application.routes.draw do
resources :users
resources :sessions
# Root Route
root :to => 'pages#home'
# Pages Routes
match '/contact', :to => 'pages#contact'
match '/about', :to => 'pages#about'
match '/help', :to => 'pages#help'
match '/signup', :to => 'users#new'
# Users Route
match '/signup', :to => 'users#new'
#Sessions Routes
get "logout" => "sessions#destroy", :as => "logout"
get "login" => "sessions#new", :as => "login"
end
And Here is my rake routes
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"}
sessions GET /sessions(.:format) {:action=>"index", :controller=>"sessions"}
POST /sessions(.:format) {:action=>"create", :controller=>"sessions"}
new_session GET /sessions/new(.:format) {:action=>"new", :controller=>"sessions"}
edit_session GET /sessions/:id/edit(.:format) {:action=>"edit", :controller=>"sessions"}
session GET /sessions/:id(.:format) {:action=>"show", :controller=>"sessions"}
PUT /sessions/:id(.:format) {:action=>"update", :controller=>"sessions"}
DELETE /sessions/:id(.:format) {:action=>"destroy", :controller=>"sessions"}
root / {: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"}
/signup(.:format) {:controller=>"users", :action=>"new"}
logout GET /logout(.:format) {:action=>"destroy", :controller=>"sessions"}
login GET /login(.:format) {:action=>"new", :controller=>"sessions"}
This all results in the following error:
1) UsersController POST 'create' success should redirect to the user show page
Failure/Error: response.should redirect_to(user_path(assigns(:user)))
ActionController::RoutingError:
No route matches {:action=>"show", :controller=>"users"}
# ./spec/controllers/users_controller_spec.rb:95:in `block (4 levels) in <top (required)>'
Any ideas on what I'm doing wrong?
It looks like to me that the show action isn't getting the user information it needs to get the correct page. The assigns method is just creating an instance variable. The user_path call will need a User mock or object to make the call work correctly.