I get this error:
Failures:
1) UsersController DELETE 'destroy' should sign a user out
Failure/Error: delete :destroy
ActionController::UrlGenerationError:
No route matches {:controller=>"users", :action=>"destroy"}
My test is:
it "should sign a user out" do
test_sign_in(Factory(:user))
delete :destroy
expect(controller).to_not be_signed_in
expect(response).to redirect_to(root_path)
end
The test_sign_in function is in the spec helper:
def test_sign_in(user)
controller.sign_in(user)
end
My rake routes:
Prefix Verb URI Pattern Controller#Action
sessions_new GET /sessions/new(.:format) sessions#new
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 / pages#home
contact GET /contact(.:format) pages#contact
about GET /about(.:format) pages#about
help GET /help(.:format) pages#help
signup GET /signup(.:format) users#new
signin GET /signin(.:format) sessions#new
signout GET /signout(.:format) sessions#destroy
pages_home GET /pages/home(.:format) pages#home
Anyone knows how can I solve this error?
Your route is defined as
DELETE /users/:id(.:format) users#destroy
which means that the route is expecting something like
DELETE /users/4
Looking at your test, you are just requesting DELETE /users, this was derived from this error message:
ActionController::UrlGenerationError: No route matches {:controller=>"users", :action=>"destroy"})
So, you need to modify your test to handle the :id part of the route. This is un-tested, but you're roughly looking for:
user = Factory(:user)
test_sign_in(user)
delete :destroy, id: user.id
ooooh god!! I found the error. I accidentally was writing the code in the users_controller_spec and I should do this in the sessions_controller_spec.
Thanks Andreas for trying to help!
Related
I am working on a project which includes signup, login and users can post articles. However I want to include username of the user in article url. So I did like this in routes.rb
scope '/:username' do
resources :articles, :path => '/status', only: [:create, :destroy, :show]
end
Now when I open project index which shows all articles, I am getting this error.
No route matches {:action=>"create", :controller=>"articles"} missing required keys: [:username]
Am I doing something wrong?
Edit:
Here is the output of 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
root GET / static_pages#home
help GET /help(.:format) static_pages#help
signup GET /signup(.:format) users#new
login GET /login(.:format) sessions#new
POST /login(.:format) sessions#create
logout DELETE /logout(.:format) sessions#destroy
GET / users#index
POST / users#create
GET /new(.:format) users#new
GET /:id/edit(.:format) users#edit
GET /:id(.:format) users#show
PATCH /:id(.:format) users#update
PUT /:id(.:format) users#update
DELETE /:id(.:format) users#destroy
archings POST /:username/status(.:format) articles#create
arching GET /:username/status/:id(.:format) articles#show
DELETE /:username/status/:id(.:format) articles#destroy
Here is the code of routes.rb
Rails.application.routes.draw do
root 'static_pages#home'
get 'help' => 'static_pages#help'
get 'signup' => 'users#new'
get 'login' => 'sessions#new'
post 'login' => 'sessions#create'
delete 'logout' => 'sessions#destroy'
resources :users, shallow: true do
resources :articles
end
end
Although having really short URLs may be attractive you should not use a wildcard at the "lowest level" of your url schema - unless your application only really deals with a single type of resource. Adding a wildcard at the bottom level in your urls causes huge headaches since it will swallow every other route unless great care is taken.
I would really recommend that you use /users/:username/articles.
resources :users, shallow: true do
resources :articles
end
This is a better restful design and it means that you do not have to blacklist usernames against a list of the other urls (and future urls) needed in your application.
Added:
The output of $ rake routes:
Prefix Verb URI Pattern Controller#Action
user_articles GET /users/:user_id/articles(.:format) articles#index
POST /users/:user_id/articles(.:format) articles#create
new_user_article GET /users/:user_id/articles/new(.:format) articles#new
edit_article GET /articles/:id/edit(.:format) articles#edit
article GET /articles/:id(.:format) articles#show
PATCH /articles/:id(.:format) articles#update
PUT /articles/:id(.:format) articles#update
DELETE /articles/:id(.:format) articles#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
PATCH /users/:id(.:format) users#update
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy
I am trying to integration test a user login system using the built-in test framework.
I am writing a test to make sure a user cannot edit another user's info. The test looks like this:
test "user can't edit other user's info" do
login(:account_administrator)
get new_user_path
assert_response :success
assert_difference("User.count") do
post_via_redirect users_path, user: {first_name: "Bob", last_name: "Jones", email: "bob#jones.com", password: "password", password_confirmation: "password", is_team_member: false, is_direct_manager: false, is_senior_leadership: false, is_reviewer: false, is_account_administrator: false}
end
assert_equal user_path(assigns(:user)), path
the_id = assigns(:user).id
logout
login(:team_member)
get edit_user_path, {id: the_id} ### test fails on this line ###
logout
end
The test fails on the marked line with this error:
1) Error:
test_user_can't_edit_other_user's_info(UserFlowsTest):
ActionController::RoutingError: No route matches {:action=>"edit", :controller=>"users"}
But when I do rake routes this is what I get:
login GET /login(.:format) sessions#new
logout GET /logout(.:format) sessions#destroy
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
PUT /sessions/:id(.:format) sessions#update
DELETE /sessions/:id(.:format) sessions#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
root / users#index
If I change get edit_user_path, {id: the_id} (the line that fails) to get users_path the error goes away, but that is not what I'm trying to test.
I have the following routes.
$ rake routes
Prefix Verb URI Pattern Controller#Action
password_resets_new GET /password_resets/new(.:format) password_resets#new
password_resets_edit GET /password_resets/edit(.:format) password_resets#edit
account_activations_edit GET /account_activations/edit(.:format) account_activations#edit
root GET / home_page#index
signup GET /signup(.:format) users#new
login GET /login(.:format) sessions#new
POST /login(.:format) sessions#create
logout DELETE /logout(.:format) sessions#destroy
following_user GET /users/:id/following(.:format) users#following
followers_user GET /users/:id/followers(.:format) users#followers
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
GET /users(.:format) users#index
POST /users(.:format) users#create
GET /users/new(.:format) users#new
GET /users/:id/edit(.:format) users#edit
GET /users/:id(.:format) users#show
PATCH /users/:id(.:format) users#update
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy
edit_account_activation GET /account_activations/:id/edit(.:format) account_activations#edit
password_resets POST /password_resets(.:format) password_resets#create
new_password_reset GET /password_resets/new(.:format) password_resets#new
edit_password_reset GET /password_resets/:id/edit(.:format) password_resets#edit
password_reset PATCH /password_resets/:id(.:format) password_resets#update
PUT /password_resets/:id(.:format) password_resets#update
microposts POST /microposts(.:format) microposts#create
micropost DELETE /microposts/:id(.:format) microposts#destroy
relationships POST /relationships(.:format) relationships#create
relationship DELETE /relationships/:id(.:format) relationships#destroy
$
When the user clicks the Activate account link in the mailer sent to the user, I would like the user to be redirected to edit_user so that he can update his profile. Right now the user gets redirected to his index page as shown below.
class AccountActivationsController < ApplicationController
def edit
user = User.find_by(email: params[:email])
if user && !user.activated? && user.authenticated?(:activation, params[:id])
user.activate
flash[:success] = "Account activated!"
log_in user
redirect_to user
else
flash[:danger] = "Invalid activation link"
redirect_to root_url
end
end
end
How should I change the redirect_to user to accomplish this?
You can just do...
redirect_to edit_user_path user
That will display the edit user view for the user "user".
Cheers
I have a search page and a search button on it, and when I click on the search button, it gave me an error below:
No route matches [POST] "/searching"
And I want that when I click on the search button, It goes into the search page, and show result of search. below is search page controller:
def search
#students=Students.all
#blah = params[:tf_Zip]
puts #blah
if ( !params[:tf_Zip].blank? or params[:tf_Zip] !="" )
#user_zip = User.where(user_Zip: params[:tf_zip])
end
render 'search'
end
You can see above that I am render here to the search page, and below is routes.rb page:
resources :search, only: [:search, :create]
match '/searching', to: 'search#search', via: 'get'
And below is the rake routes:
Helper HTTP Verb Path Controller#Action
Path / Url
search_new_path GET /search/new(.:format) search#new
search_create_path GET /search/create(.:format) search#create
settings_new_path GET /settings/new(.:format) settings#new
educations_create_path GET /educations/create(.:format) educations#create
educations_destroy_path GET /educations/destroy(.:format) educations#destroy
professions_create_path GET /professions/create(.:format) professions#create
professions_destroy_path GET /professions/destroy(.:format) professions#destroy
communications_create_path GET /communications/create(.:format) communications#create
communications_destroy_path GET /communications/destroy(.:format) communications#destroy
availabilities_create_path GET /availabilities/create(.:format) availabilities#create
availabilities_destroy_path GET /availabilities/destroy(.:format) availabilities#destroy
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
sessions_path POST /sessions(.:format) sessions#create
new_session_path GET /sessions/new(.:format) sessions#new
session_path DELETE /sessions/:id(.:format) sessions#destroy
availabilities_path POST /availabilities(.:format) availabilities#create
availability_path DELETE /availabilities/:id(.:format) availabilities#destroy
communications_path POST /communications(.:format) communications#create
communication_path DELETE /communications/:id(.:format) communications#destroy
professions_path POST /professions(.:format) professions#create
profession_path DELETE /professions/:id(.:format) professions#destroy
educations_path POST /educations(.:format) educations#create
education_path DELETE /educations/:id(.:format) educations#destroy
settings_path POST /settings(.:format) settings#create
new_setting_path GET /settings/new(.:format) settings#new
search_index_path POST /search(.:format) search#create
root_path GET / static_pages#home
signup_path GET /signup(.:format) users#new
signin_path GET /signin(.:format) sessions#new
signout_path DELETE /signout(.:format) sessions#destroy
default_path GET /default(.:format) static_pages#default
GET /availabilities(.:format) availabilities#new
GET /communications(.:format) communications#new
GET /professions(.:format) professions#new
GET /educations(.:format) educations#new
GET /settings(.:format) settings#new
searching_path GET /searching(.:format) search#search
Kindly help me, waiting for your reply. Thanks
try this one, as you're sending your form with POST
match '/searching', to: 'search#search', via: 'post'
I am doing Michael Hartl's Ruby on Rails Tutorial and I am getting a routing error.
Here is the terminal output after doing 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
Routing error is as below
when localhost:3000/signin/ OR localhost:3000/sessions/new
No route matches {:action=>"destroy", :controller=>"sessions"}
Does anybody have an answer on how to correct the route settings?
Do you have a line like this in your routes file?
match '/signout', :to => 'sessions#destroy'