routing error on Ruby on Rails - ruby-on-rails

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'

Related

Rails - new data shows only in development mode can't figure out how to migrate it to production mode

I have been following Michael Hartl's Rails tutorial. I am at the beginning of chapter 8 with a brand new login page. At the end of Chapter 7 i had switched from the default WEBrick server to puma. Everything worked fine, i had the same result in production as i had in development mode. However, at the beginning of chapter 8 a login page was added so a session controller was created, added GET/POST/DELETE /login to routes.rb, updated session_controller_test.rb and created a sessions/new.html.erb for the login form. The login form shows in the development mode but i can't get it show in production mode. I tried heroku reset, heroku run rake db:migrate. I restarted the server. I added two new users to the app and ran rails c production but no users were found. heroku logs result: ActionController::RoutingError (No route matches [GET] "/sessions"):
rails routes: Prefix Verb URI Pattern Controller#Action
sessions_new GET /sessions/new(.:format) sessions#new
users_new GET /users/new(.:format) users#new
root GET / static_pages#home
help GET /help(.:format) static_pages#help
about GET /about(.:format) static_pages#about
contact GET /contact(.:format) static_pages#contact
signup GET /signup(.:format) users#new
POST /signup(.:format) users#create
login GET /login(.:format) sessions#new
POST /login(.:format) sessions#create
logout DELETE /logout(.: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
PATCH /users/:id(.:format) users#update
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy
I am new to coding. I have been trying to figure this out but it beats me. Any idea, please?

Username in urls - Ruby on Rails

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

Rails4 - How to redirect user to edit_user after accout activation?

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

Michael Hartl Tutorial, no routes match

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!

Making Rails routing beautiful

I have the following in my Rails routes.rb:
resource :sign_up, only: [:new, :create]
resources :users
get 'users/activate/:token' => 'users#activate', as: 'activate_user'
Which gives me the following routes:
Prefix Verb URI Pattern Controller#Action
sign_up POST /sign_up(.:format) sign_ups#create
new_sign_up GET /sign_up/new(.:format) sign_ups#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
activate_user GET /users/activate/:token(.:format) users#activate
I'd like to get rid of the get 'users/activate/:token' ... route and use nesting or scoping instead, though I can't figure it out. Is there a way to accomplish this?
Thanks!
You can set up a collection route for users:
resources :users do
collection do
get 'activate/:token', :action => :activate, :as => :activate
end
end
And it will give you routes like this:
Prefix Verb URI Pattern Controller#Action
activate_users GET /users/activate/:token(.:format) users#activate
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

Resources