Rails devise 4 matched routes - ruby-on-rails

i have hit an error with devise, on rails 4 in terms of routing. I wish to have the /users/sign_up route matched to /login. the match :to method yielded no results.
thank you in advance

Add the following to your config/routes.rb:
devise_scope :user do
get '/login', to: 'devise/sessions#new'
end

Related

How to use multiple root route in rails 6?

Just updated to rails 6 and having trouble with a conditional root route
devise_scope :user do
authenticated do
root to: 'users#show'
end
unauthenticated do
root to: 'visitors#index'
end
end
I've attempted to check for the logged-in user in visitors#index and do a redirect to users#show but now I have this ugly URL '/users/:id' instead of being able to visit users#show with a clean root URL.
In my case I was able to solve this by adding an :as argument:
devise_scope :user do
authenticated do
root to: 'users#show'
end
unauthenticated do
root to: 'visitors#index', as: :visitors_url
end
end
I think the reasoning behind the change is to make it so that Rails knows where to route root_url.
Note: I found the answer on reddit where it was suggested using namespace would be cleaner.
namespace :visitors, path: nil do
root to: 'visitors#index'
end
I don't know if there is a way to make that work with devise though.

Ruby on Rails Devise id='sign up'

So a few people have had this problem, but none of their code actually matches mine so their solutions aren't working.
Basically, whenever I try to access /users/:id it passes in the params correctly {'id' => '1'}, but then the page errors out on 'no user with id='sign_out'
My routes:
Rails.application.routes.draw do
devise_for :users, path: 'devise'
get 'sessions/new'
get 'intro', to: 'index#intro'
root 'index#intro'
authenticated :user do
root 'index#homepage', as: :authenticated_root
end
devise_scope :user do
get '/start', to: 'devise/sessions#new', as: "login"
get '/devise/sign_out', to: 'devise/sessions#destroy', as: "logout"
get '/signup', to: 'users#new', as: "signup"
end
get '/forgotPassword', to: 'index#forgotPassword'
get '/homepage', to: 'index#homepage'
get '/meetUs', to: 'index#meetUs'
get '/makeSuggestion', to: 'index#makeSuggestion'
get 'profile', to: 'index#profile'
resources :suggestions
resources :rewards
resources :users
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end
Code in controller erroring out:
# GET /users/1
# GET /users/1.json
def show
#users = User.find(params[:id])
end
I'm pretty sure the issue has something to do with the routes thinking /users is both a resource of mine and of devise. But I don't know how to tell it to use the default route rails made as opposed to the one devise made.
If anyone could help, and try and explain why it's happening, that'd be great. Thanks in advance!
edit: Picks of the error and trace

Michael Hartl ch9 : how to sign up

I’ve completed Michael Hartl ch 9 and have no error in testing.
But I could not sign up...
It returns error as below even so previously I be able to sign up..login works fine.
How to fix it and be able to sign up?
Should I merge sign up branch? But doing that I could loose all changes.. Could something be done about sign up form this more advance branch?
Error:
No route matches [POST] "/signup"
routes.rb:
Rails.application.routes.draw do
root 'static_pages#home'
get '/help', to: 'static_pages#help'
get '/about', to: 'static_pages#about'
get '/contact', to: 'static_pages#contact'
get '/signup', to: 'users#new'
get '/login', to: 'sessions#new'
post '/login', to: 'sessions#create'
delete '/logout', to: 'sessions#destroy'
resources :users
end
thanks.
Thanks everybody for answers.
It works now.
One more question related.
When users sign up - they could directly participate in website. It is no good since they need to get confirmation by email and only that they could participate.
How it could be implemented?
I think it is in the solutions Michael Hartl and I even implemented it for Rails 4, but could not remember what need to change..
What files need to be changed to allow users participate in website only after getting confirmation by email.
thanks.
The error is very clear and crisp, you don't have a route for users#create, you need to implement that action and it should work.
Go through this section in the book..
No route matches [POST] "/signup"
You should have post '/signup', to: 'users#create' in your routes.
You need to add
post '/signup', to: 'users#create'
And write create method in
class UsersController < ApplicationController
def create
# Create User with signup parameterss
end
end

Devise users/sign_in and users/show action trouble

I user devise 3.4.0 under rails 4.1.0.
I want to add user detail page, so I made this route
get 'users/:id' => 'users#show', as: 'user'
But after this, when I access /users/sign_in path, it try to find the user show page.
How to write the right route?
What you did will actually "override" the devise routes (and i think this is the problem you are facing)
If you want to add another route in the scope of devise routes, you have to do something like :
devise_scope :user do
get '/users/:id' => 'users#show'
end
after
devise_for :users
Let me know if it solves the problem !

Rails routing to multiple route

I'm using devise for rails.
I have the following route for devise.
devise_for :user
Which routes to 'user/sign_in' and several other.
So I want to change this route to: get 'login'. Is this possible?
I tried doing
match 'login', to: 'user/sign_in', via: :get
Which did not work as well, what am I doing wrong, and what does the above code do?
To use /login for sign_in add the following to your config/routes.rb:
devise_scope :user do
get 'login', to: 'devise/sessions#new'
end
This'll work:
devise_for :user, :path => 'login'
You might need :users and not :user, FYI.

Resources