Routing Error uninitialized constant controller - ruby-on-rails

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.

Related

How can I correct the routing error in Ruby on Rails app

I always get routing errors in this Ruby file on Rails to project. It shows me routing error as you can see in below screenshot.
All gems are installed properly it shows me the home page but do not show me the check file or show file page.
Here is my Routes.rb code:
Rails.application.routes.draw do
get 'home/submission3'
post 'home/checkFile'
get 'home/showFiles'
# For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
end
Here is an image of the error shown:
Here is my controller code:
# C:\Users\attari\RubymineProjects\submission3\app\controllers\home_controller.rb
require 'docx'
require 'find'
class HomeController < ApplicationController
def submission3
end
def checkFile
word = params[:folder][:word]
path = params[:folder][:path]
#files = checkWord(word, path)
puts "FILES ARE #{#files}"
redirect_to home_showFiles_path(#files)
# puts "WORD IS #{word} PATH IS #{path}"
end
def save
#submissions = submission3.new
end
def checkWord(userInput, folderInput)
count = 0
if (Dir.exist?(folderInput))
docx_file_paths = []
filtered_files = []
Find.find(folderInput) do |path|
docx_file_paths << path if path =~ /.*\.docx$/
end
(0..docx_file_paths.length - 1).each { |i|
d = Docx::Document.open(docx_file_paths[i])
d.each_paragraph do |p|
string_ = p.to_s()
if (string_.include? userInput)
puts docx_file_paths[i]
filtered_files.append(docx_file_paths[i])
count = count + 1
puts count
else
puts "No word Matched"
end
end
puts count
}
return filtered_files
else
puts "No Directory exist"
end
end
def create
#submission3 = submission3.new(params[:word])
if #submission3.save
redirect_to new_submission_path
end
end
private
def book_params
params.require(:book).permit(:title)
end
end
How can I fix this routes.rb error?
If you look at the error message, you can see that the filename is being appended directly to the route name (/home/showFiles.C:%5CUsers...). You need to add an id to the route so that Rails knows to expect something there. Your route should look something like this:
get 'home/show_files/:file_name'
[Note that you need to define :file_name in the context where this will be accessed (your controller).]
Even better would be to use a model as the basis for your route. So, if you have (for example) a File model, you would use:
resources :files
and Rails will generate a whole pile of routes for you automatically (or you can limit the routes by doing something like resources :files, only: [:show, :index, :create, :delete]).
Basically, I think you need to read up more on how Rails, and routes specifically, are supposed to work. RailsGuides is probably a good place to start. If you work with Rails and its conventions, you'll find your life a lot easier! Rails prefers convention over configuration!
Also, on a side issue, you're not following Rails convention in naming your methods and variables. CamelCase is used in object names (like class HomeController) but not method names or variable names - Rails uses snake_case for these. Again, if you stick to Rails convention (show_files not showFiles, for example), you'll find things go a lot smoother. Following the Rails conventions means Rails will seamlessly stitch things together for you; fail to follow them and Rails will be a complete nightmare.
Based on the error, it looks like you are trying to access /submission3 route even though you have defined /home/submission3 route. Updating the route should fix your routing error.

Rails - using namespace controllers to organize files

I am trying to learn about namespacing.
I've asked a few questions on this topic previously, but I'm not understanding what is going on.
I have made a folder in my controller's folder called 'features'. In it, I have saved a file called app_roles_controller.rb.
The first line of that controller is:
class Features::AppRolesController < ApplicationController
The purpose of the features folder is so I can organise my files better (that's it).
In my routes.rb, I have tried:
resources :app_roles, :namespace => "features", :controller => "app_roles"
I have also tried:
namespace :features do
resources :app_roles
end
I have a model (top level) called app_role.rb and I have a views folder saved as views/features/app_roles which then has the index, show etc files in it. The table in my schema is called 'app_roles".
When I rake routes for app_roles, I get:
Paths Containing (app_role):
app_roles_path GET /app_roles(.:format)
app_roles#index {:namespace=>"features"}
POST /app_roles(.:format)
app_roles#create {:namespace=>"features"}
new_app_role_path GET /app_roles/new(.:format)
app_roles#new {:namespace=>"features"}
edit_app_role_path GET /app_roles/:id/edit(.:format)
app_roles#edit {:namespace=>"features"}
app_role_path GET /app_roles/:id(.:format)
app_roles#show {:namespace=>"features"}
PATCH /app_roles/:id(.:format)
app_roles#update {:namespace=>"features"}
PUT /app_roles/:id(.:format)
app_roles#update {:namespace=>"features"}
DELETE /app_roles/:id(.:format)
app_roles#destroy {:namespace=>"features"}
I can't understand what it is that I'm doing wrong.
When I try:
http://localhost:3000/app_roles#index
I get an error that says:
uninitialized constant AppRolesController
When I try:
http://localhost:3000/features/app_roles#index
I get an error that says:
No route matches [GET] "/features/app_roles"
I'm looking for a plain English explanation of how to set this up. I've tried the programming ruby book (several times over).
Please, can you help me understand what needs to happen to introduce organisational files in my rails app?
It looks like your other attempt was actually correct. As outlined in the Rails documentation, if you want to generate routes for the resource app_roles under the namespace features you can add the following to your routes.rb file:
namespace :features do
resources :app_roles
end
Now, you run rake routes you will see the following:
Prefix Verb URI Pattern Controller#Action
features_app_roles GET /features/app_roles(.:format) features/app_roles#index
POST /features/app_roles(.:format) features/app_roles#create
new_features_app_role GET /features/app_roles/new(.:format) features/app_roles#new
edit_features_app_role GET /features/app_roles/:id/edit(.:format) features/app_roles#edit
features_app_role GET /features/app_roles/:id(.:format) features/app_roles#show
PATCH /features/app_roles/:id(.:format) features/app_roles#update
PUT /features/app_roles/:id(.:format) features/app_roles#update
DELETE /features/app_roles/:id(.:format) features/app_roles#destroy
The first line for example means that if you make a GET request to /features/app_roles it will be routed to the index action in the features/app_roles controller.
In other words, if you visit http://localhost:3000/features/app_roles it will route the request to index action that is located in app/controllers/features/app_roles_controller.rb which it expects to have the class Features::AppRolesController.
So your app/controllers/features/app_roles_controller.rb file should look something like this:
class Features::AppRolesController < ApplicationController
def index
end
end

Rails routing gives file not found error

I have a Ruby on Rails application with the following entries in routes.rb:
get '/teachers/welcome', to: 'teachers#welcome'
Which means that if I type: http://localhost:3000/teachers/welcome then I should be able to see the welcome view from the teachers controller. But I keep getting File Not Found error. I'm new to Ruby so bear with me.
When I look at the application, the files are there:
app/controllers/teachers_controller.rb
app/views/teachers/welcome.html.erb
Please watch the naming of you model (without s) but your contoller with s .. and the view name have to match the action name . And run rake routes to check your working routes . + be carefull of the routes order in the routes.rb file .. and will not have any problem in your life with the routes
Assuming that you have a method(action) 'welcome' in your controller try this
get 'teachers/welcome' => 'teachers#welcome'
Make sure you have everything named properly, like the controller and the view.
app/controllers/teachers_controller.rb
class TeachersController < ApplicationController
def welcome
end
end
app/views/teachers/welcome.html.erb
You have to remove the first slash
get 'teachers/welcome' => 'teachers#welcome'
Your Controller:
# app/controllers/teachers_controller.rb
class TeachersController < ApplicationController
def welcome
end
end
Note: you can use a generator to create it automatically:
rails generate controller teachers welcome

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???

Remove Routes Specified in a Gem?

Is there a way to remove routes specified in a gem in Rails 3? The exception logger gem specifies routes which I don't want. I need to specify constraints on the routes like so:
scope :constraints => {:subdomain => 'secure', :protocol => 'https'} do
collection do
post :query
post :destroy_all
get :feed
end
end
Based on the Rails Engine docs, I thought I could create a monkey patch and add a routes file with no routes specified to the paths["config/routes"].paths Array but the file doesn't get added to ExceptionLogger::Engine.paths["config/routes"].paths
File: config/initializers/exception_logger_hacks.rb
ExceptionLogger::Engine.paths["config/routes"].paths.unshift(File.expand_path(File.join(File.dirname(__FILE__), "exception_logger_routes.rb")))
Am I way off base here? Maybe there is a better way of doing this?
It is possible to prevent Rails from loading the routes of a specific gem, this way none of the gem routes are added, so you will have to add the ones you want manually:
Add an initializer in application.rb like this:
class Application < Rails::Application
...
initializer "myinitializer", :after => "add_routing_paths" do |app|
app.routes_reloader.paths.delete_if{ |path| path.include?("NAME_OF_GEM_GOES_HERE") }
end
Here's one way that's worked for me.
It doesn't "remove" routes but lets you take control of where they match. You probably want every route requested to match something, even if it is a catch all 404 at the bottom.
Your application routes (MyApp/config/routes.rb) will be loaded first (unless you've modified the default load process). And routes matched first will take precedence.
So you could redefine the routes you want to block explicitely, or block them with a catch all route at the bottom of YourApp/config/routes.rb file.
Named routes, unfortunately, seem to follow ruby's "last definition wins" rule. So if the routes are named and your app or the engine uses those names, you need to define the routes both first (so yours match first), and last (so named routes point as you intended, not as the engine defines.)
To redefine the engine's routes after the engine adds them, create a file called something like
# config/named_routes_overrides.rb
Rails.application.routes.draw do
# put your named routes here, which you also included in config/routes.rb
end
# config/application.rb
class Application < Rails::Application
# ...
initializer 'add named route overrides' do |app|
app.routes_reloader.paths << File.expand_path('../named_routes_overrides.rb',__FILE__)
# this seems to cause these extra routes to be loaded last, so they will define named routes last.
end
end
You can test this routing sandwich in the console:
> Rails.application.routes.url_helpers.my_named_route_path
=> # before your fix, this will be the engine's named route, since it was defined last.
> Rails.application.routes.recognize_path("/route/you/want/to/stop/gem/from/controlling")
=> # before your fix, this will route to the controller and method you defined, rather than what the engine defined, because your route comes first.
After your fix, these calls should match each other.
(I posted this originally on the refinery gem google group here: https://groups.google.com/forum/?fromgroups#!topic/refinery-cms/N5F-Insm9co)

Resources