No route matches [POST] "/sessions/user" with devise - ruby-on-rails

When i try to log in using devise on my rails app, I get the following error.
No route matches [POST] "/sessions/user"
I have tried other solutions that I have found here but they don't seem to work.
My routes.rb has the following in the file.
Rails.application.routes.draw do
devise_for :users
resources :non_availabilities
resources :sessions
resources :programs
resources :activities
resources :locations
resources :employees
resources :expertises
resources :emp_classifications
resources :educations
get 'dashboard/index'
root 'dashboard#index'
end.
How do I fix this?

Can you add this line to your routes
match '/sessions/user', to: 'devise/sessions#create', via: :post

I faced the exact same situation (my app has a Session model and SessionsController too). This solution worked for me, with a tiny modification. The code below was added to config/routes.rb:
devise_scope :user do
match '/sessions/user', to: 'devise/sessions#create', via: :post
end

Related

Cannot logout of rails application devise gives error Could not find devise mapping for path "/tuners/sign_out

I have this issue when loging out of devise:
Could not find devise mapping for path "/tuners/sign_out". This may happen for two reasons: 1) You forgot to wrap your route inside the scope block. For example: devise_scope :user do get "/some/route" => "some_devise_controller" end 2) You are testing a Devise controller bypassing the router. If so, you can explicitly tell Devise which mapping to use: #request.env["devise.mapping"] = Devise.mappings[:user]
My routes file
Rails.application.routes.draw do
devise_for :tuners
get '/tuners/sign_out' => 'devise/sessions#destroy'
resources :hardy_pianos
devise_for :admins
resources :images
resources :entries
resources :models
get '/clients/mail_room' => 'clients#mail_room'
get '/clients/mail' => 'clients#mail'
#list of clients to reschedule
get '/clients/book_again' => 'clients#book_again'
# current upcoming jobs
get '/clients/current' => 'clients#current'
# reports
get '/clients/reports' => 'clients#reports'
resources :clients
seems_rateable
post '/rate' => 'rater#create', :as => 'rate'
resources :technicians
resources :members
resources :admins
resources :tuners
get 'tuners/tuners_pianos'
get 'pianos/tuners_pianos'
resources :tuners
resources :journals
resources :expenses
resources :interests
resources :purchases
resources :contacteds
resources :pianos
get '/pianos/work_needed' => 'pianos#work_needed'
get '/invoices/work_needed' => 'invoices#work_needed'
resources :invoices
resources :pages
#devise_for :installs
resources :prosperities
get '/finances/taxes' => 'finances#taxes'
resources :posts
get '/tunings/current' => 'tunings#current' # or match for older Rails versio
get '/tunings/bookagain' => 'tunings#bookagain' # or match for older Rails version
get '/tunings/bookagain12' => 'tunings#bookagain12'
get '/tunings/reports' => 'tunings#reports'
get '/tunings/information' => 'tunings#information'
resources :tunings
root 'pages#index'
get '/' => 'tunings#current'
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end
Can anyone help me understand what the issue is here with this? I'm kind of stuck with this and have googled for a while but left it on the back burner. Thanks in advance.
Replace the line:
get '/tuners/sign_out' => 'devise/sessions#destroy'
with
delete 'sign_out', to: 'devise/sessions#destroy'
or can you try this this. watch the order of lines.:
Rails.application.routes.draw do
devise_scope :tuners do
get "sign_out", to: "devise/sessions#destroy"
end
devise_for :tuners

devise (No routes Matches [put] "/users/edit.user)

ruby 2.3.3,
rails 5.1.6.1
I am trying to edit a user, after i click on the update, my app loads to this:
No route matches [PUT] "/users/edit.user"
# routes.rb
Rails.application.routes.draw do
devise_for :users
resources :posts
resources :projects
resources :contacts, only: [:new, :create]
get 'welcome/index'
root 'welcome#index'
get '*path' => redirect('/')
end
seems to be a problem with your routes.rb file
check this guide -> https://guides.rubyonrails.org/v5.0/routing.html#resources-on-the-web
from what I can tell you are not defining the routes pointing to your UsersController (devise_for :users doesn't do that, it defines completely other set of routes)
your routes.rb should look roughly like below:
Rails.application.routes.draw do
resources :users
...some other routes (like devise_for :users)
end
resources :users will define routes like /users for showing all user resource or /users/:id/edit for editing the user resource.
Now that you have those routes the will point to the UsersController class which should be defined in the users_controller.rb. In the controller you should define appropriate actions (like edit or update) that will update your user resource
I hope this is going to help solving your problem.

Uninitialized constant Admin::Admin

Using Ruby: 2.3.1p112 and Rails: 3.2.12
I'm trying call a demo method in my controller. So, in my _form.html.erb I have:
<%= link_to 'Demo', "/admin/clinics/"+#clinic.id.to_s+"/demo" %>
In my routes.rb:
match "/admin" => "admin#index", :as => :admin
namespace :admin do
resources :admin_users
resources :health_plan_tables
resources :health_aid_tables
resources :clients
resources :clinics
resources :specialties
resources :qualifications
resources :profissionals
resources :addresses
resources :documents
resources :banners
root :to => 'banners#index'
get 'logout' => 'devise/sessions#destroy'
get 'clinics/:id/demo', to: 'admin/clinics#demo', as: 'demo'
end
My clinics_controller.rb is inside the folder controllers/admin, and I just have:
def demo
print "hello"
end
So, when I click on link, error message appears Uninitialized constant Admin::Admin.
Any ideia how to fix it?
Since you are already defining your demo route inside a namespace there is no need to specify admin/clinics#demo, just clinics#demo will be necessary:
namespace :admin do
resources :admin_users
resources :health_plan_tables
resources :health_aid_tables
resources :clients
resources :clinics
resources :specialties
resources :qualifications
resources :profissionals
resources :addresses
resources :documents
resources :banners
root :to => 'banners#index'
get 'logout' => 'devise/sessions#destroy'
get 'clinics/:id/demo', to: 'clinics#demo', as: 'demo'
end
According to the error log you're looking for a controller namespaced under admin/admin/clinics (it says that in the controller part of the params).
Change the bottom route to not include admin (it's already namespaced and you're effectively namespacing it twice):
get 'clinics/:id/demo', to: 'clinics#demo', as: 'demo'
This will route to the correct controller, admin/clinics, instead of admin/admin/clinics

The action x could not be found for xyzController

I'm a beginner in Ruby on Rails and I have a problem. I'm trying to add comments to my app. Everything is working, but when I added this code to routes.rb
resources :galleries do
resources :comments, module: :galleries
end
resources :articles do
resources :comments, module: :articles
end
I can't update any gallery or article. My whole routes.rb:
Rails.application.routes.draw do
devise_for :users
resources :galleries do
resources :comments, module: :galleries
end
resources :articles do
resources :comments, module: :articles
end
match ':controller(/:action(/:id))(.:format)', via: [:post, :get]
root 'public#index'
end
It looks like the request (articles/12?page_id=4) is falling through to the match statement and not getting picked up by your resources. Your match statement:
match ':controller(/:action(/:id))(.:format)', via: [:post, :get]
is matching "articles" as the controller, and looking for "12" as the action and it clearly can't find it.
I would scrap the match statement and go strictly with named routes or resourceful routing permanently, or at least while debugging. Next, run
rake routes
and check the results. From your routes.rb I am guessing you will have a line like this:
article GET /articles/:id(.:format) articles#show
If that is indeed the case, make sure you have the show() method defined in the articles controller, and verify that the logs are showing a GET request for it.

How to change a route name rails 4

I changed the routing of posts#index to match blog and I now get /blog in the URL which I was trying to accomplish.
I've tried several different things to get my actual blog post which the route currently looks something like /posts/this-is-a-test to also use blog rather than posts in the URL.
Below is my current route file. I am using the friendly_id gem, if that makes any difference in answering this question.
resources :posts do
resources :comments
end
resources :contacts, only: [:new, :create]
root "pages#home"
get "/home", to: "pages#home", as: "home"
get "about" => 'pages#about'
get "pricing" => 'pages#pricing'
get "contact_us" => 'pages#contact_us'
match 'blog', to: 'posts#index', via: :all
end
path option along with resource must help.
resources :posts, :path => 'blogs' do
resources :comments
end
This will change all /posts and /post to /blogs/ and /blog.
If you want to change your route's helper methods such as posts_path to blogs_path and new_post_path to new_blog_path etc, you can change it with as tag.
resources :posts, :path => 'blogs', :as => 'blogs' do
resources :comments
end
Or yet better, you can specify the controller and route blogs directly as:
resources :blogs, controller: 'posts' do
resources :comments
end
This is the awesomeness of Rails! :)
match 'blog/:id' => 'posts#show'
should work. But if you want to match every method in posts controller to blog (and you don't want to use the posts path), I would just rename the controller to blog instead and add resource :blog in the routes.
This helped me, from official guides documentation, add this in your routes.rb file:
get '/patients/:id', to: 'patients#show', as: 'patient'

Resources