Rails Devise - Routes Error - ruby-on-rails

I was installing the Devise gem, and everything seemed to be going according to tutorial. I got to the point where I was able to register a user, but then at some point I'm not sure what happened and I started getting routes errors:
No route matches [GET] "/projects"
Except, my routes file has been unchanged and has quite a few routes in it.
Any suggestions?
As requested the result of rake routes:
http://pastebin.com/FB17DhYq

If you can't restart/reload the server than try to reload the routes adding to the controller (some that works and you can hit it):
Rails.application.reload_routes! for Rails4 or ActionController::Routing::Routes.reload! for previous versions.
You can add just the line above or use this:
before_filter :reload_routes
private
def reload_routes
Rails.application.reload_routes! # for Rails 4
# ActionController::Routing::Routes.reload! # for previous
end

Related

Devise authenticate_admin_user! does not work after upgrading to rails 5

I have an old, rather big Rails app I need to upgrade to a current version. It is currently running on Rails 4.2.11. I've managed to upgrade all my gems now, so it runs Rails version 5.0.7. And I am in a state where the app starts again and mostly works. While doing so, I've upgraded the devise gem from version 3.4.0 to 4.0.0, but I've also tried 4.7.3. It does not make a difference to my problem.
The only thing which does not work correctly is authentication. I can load the login screen and login with a user. The login is successful, but then I get redirected back to the main application page, instead of the protected resource.
From what I could found out, the Devise session is not persisted in the session, but I don't understand why it does not work. I don't get any error in the log. The log displays the initial 401 error when I request the protected resource, and we are redirected to the login form (as expected). After a successful login (I see the sign_in_count increase in the database), a redirect to the home page happens, instead of the protected resource.
I've added the following code into the index method of the main page controller (to which I get redirected):
class MainController < ApplicationController
def index
puts "Current Admin User: #{current_admin_user} nil: #{current_admin_user.nil?} signedIn: #{admin_user_signed_in?}"
# rest of the code omitted for simplicity
end
end
The output is as follows:
web_1 | [pid: 1] [c48b7285-3f9e-4cb7-94ba-64b6c9d9bd0e] Processing by MainController#index as HTML
web_1 | Current User: is nil: true signed_in: false
The (simplified) routes.rb file looks like this:
root 'main#index'
devise_for :admin_users
namespace :admin do
constraints(CheckIp.new) do
devise_scope :admin_user do # a
root to: '/admin/main#index' # b
resources :main_admin, path: :main do
... # contains sub resources
end
end
end
end
I've added the lines a and b after the upgrade in the hope it fixes my issues, but I could not see any difference. My understanding is that the devise 4 should redirect to the root (line b) inside my scope, but this is not happening. I also tried to move the line a before the constraints check and again before the admin namespace. The results are the same in all cases.
Routes have priority in the order they are defined.
Since root 'main#index' was defined at the top of the file Rails will already match the request for / before it gets to your second route with the constraint.
All you have to do is move the default route below the constraint:
devise_for :admin_users
namespace :admin do
constraints(CheckIp.new) do
devise_scope :admin_user do # a
root to: '/admin/main#index' # b
resources :main_admin, path: :main do
... # contains sub resources
end
end
end
end
root 'main#index'
That way it "falls through" if the constraint or devise_scope does not produce a matching route.
I've finally found the reason for my issues. I've made some modification to the middleware stack for log tagging like this:
Rails.configuration.middleware.delete(ActionDispatch::Cookies)
Rails.configuration.middleware.delete(ActionDispatch::Session::CookieStore)
Rails.configuration.middleware.insert_before(Rails::Rack::Logger, ActionDispatch::Session::CookieStore)
Rails.configuration.middleware.insert_before(ActionDispatch::Session::CookieStore, ActionDispatch::Cookies)
This does not longer work. So for the time being I remove the log tagging, as authentication is more important.

Routing Error uninitialized constant controller

I'm am trying to learn Ruby on rails and I keep getting this error.
My controller is
class Clasa9Controller < ApplicationController
def multimi
end
def progresii
end
def functii
end
def vectori
end
def trigonometrie
end
def geometrie
end
end
clasa9.html.erb
<button class="btn"><%= link_to "", multimi_path %></button>
rails routes:
multimi GET /clasa_9/multimi(.:format) clasa_9#multimi
progresii GET /clasa_9/progresii(.:format) clasa_9#progresii
functii GET /clasa_9/functii(.:format) clasa_9#functii
vectori GET /clasa_9/vectori(.:format) clasa_9#vectori
trigonometrie GET /clasa_9/trigonometrie(.:format) clasa_9#trigonometrie
geometrie GET /clasa_9/geometrie(.:format) clasa_9#geometrie
and routes.rb
get 'clasa_9/multimi', to:"clasa_9#multimi", as:"multimi"
get 'clasa_9/progresii', to:"clasa_9#progresii", as:"progresii"
get 'clasa_9/functii', to:"clasa_9#functii", as:"functii"
get 'clasa_9/vectori', to:"clasa_9#vectori", as:"vectori"
get 'clasa_9/trigonometrie', to:"clasa_9#trigonometrie", as:"trigonometrie"
get 'clasa_9/geometrie', to:"clasa_9#geometrie", as:"geometrie"
devise_for :users
get 'pages/home'
get 'pages/clasa9'
get 'pages/clasa10'
get 'pages/clasa11'
get 'pages/clasa12'
get 'pages/about'
root 'pages#home'
and im am getting
Routing Error
uninitialized constant Clasa9Controller
I tried to solve this by looking up what is already posted here but I just can't solve it... I don't understand what I should change.
If your file is located inside the app/controllers folder, then it is probably a file name issue. Your file should have the name clasa9_controller.rb.
If not, then you should load the file by creating an initializer or by adding an autoload_path inside config/development.rb
Rails loads by default:
All subdirectories of app in the application and engines present at boot time. For example, app/controllers. They do not need to be the default ones, any custom directories like app/workers belong automatically to autoload_paths.
Any existing second level directories called app/*/concerns in the application and engines.
The directory test/mailers/previews.
Look it would be clasa9 but why that when you run this with the underscore method like this
Loading development environment (Rails 5.1.4)
2.3.4 :001 > "Clasa9Controller".underscore
=> "clasa9_controller"
it returns clasa9_controller that means your controller is clasa9 not clasa_9 and file name will be clasa9_controller.rb then your routes would be to: "clasa9#multimi" like this
get 'clasa_9/multimi', to: "clasa9#multimi", as: "multimi"
#or
#get 'clasa_9/multimi', to: "clasa9#multimi", as: :multimi # removed doublw quotes from multimi
...
Follow this it should work.

Dynamically add route to beginning of rails routes

For a testing helper I am working on I need to inject a route into the beginning of the routes. To add it at the end is easy:
test_routes = Proc.new do
get "/#{route_root}/:id", to: "#{route_root}#test"
end
Rails.application.routes.eval_block(test_routes)
The problem is that often there are "catch all" routes at the end of the application routes list, so I need to inject this as the FIRST route.
I have been using the technique here: How to dynamically add routes in Rails 3.2
but its hacky, and after a gem update last night it broke (not sure why yet, but its breaking inside of the routes.clear!) so I am looking for a less hacky solution.
Why not just create your own module like this:
module DynamicRouter
def self.routes router
router.get "/#{route_root}/:id", to: "#{route_root}#test"
end
end
and in your routes.rb just call your function:
# in routes.rb
# first line
DynamicRouter::routes(self)
That's how we do with some of our custom gems. You can also monkey patch the router, but it's more hacky.

Auto-generate routes for scaffolded controller in Rails 4?

I'm trying to get a quick-and-dirty Ajax UI going for an app that already has its data model well in hand - it's basically been managed via rails console so far. Anyway, I thought I would start by auto-generating the missing controller logic that you would get from a rails g scaffold, only instead with rails g scaffold_controller for an existing controller.
It created the controller, and the views, and the assets.. but it didn't touch the routes at all! It didn't even try, didn't say "warning: routes.rb has been modified, not changing" or anything like that, and there's no mention of routes at all in the help output of rails g scaffold_controller.
So how do I say "Just give me the normal routes you would have given me if I started from scratch, please!"?
If I understand the question:
Please, open the config/routes.rb file, and inside the block (routes.draw) add the resources method with the table name (plural of model) as param. Like this:
MyApp::Application.routes.draw do
resources :products
... # rest of code
end
That define the routes for RESTful actions over products. You can read more here
At the console you can run: rake routes to see the available routes at your app.
Although this is asking about Rails 4 for long time ago, but with Rails 5, rails g scaffold_controller still won't auto-generate route, I did it with below monkey patch:
require 'rails/generators/rails/scaffold_controller/scaffold_controller_generator'
patcher = Module.new do
extend ActiveSupport::Concern
included do
hook_for :resource_route, required: true
end
end
Rails::Generators::ScaffoldControllerGenerator.send :include, patcher

Devise: My customer data is not available anymore

I made a big mistake and I'm not sure how to fix it.
I originally installed Devise the proper way, but today I decided to create a second instance of user using the following terminal line:
rails g controller Users index
Now I realize this was not the right thing to do, and I decided to destroy the generated code as with this command:
rails destroy controller Users
Now nothing works and I get the following error when accessing the sign up view:
Routing Error
uninitialized constant CustomersController
Try running rake routes for more information on available routes.
Here's my route file
devise_for :users
root :to => 'events#index'
What am I supposed to do in this case???

Resources