Added new Rails controller using "generate controller" but can't load page - ruby-on-rails

I have a Rails app that has only consisted of backend code so far (a little custom workflow engine, Redis, foreman, etc). Today I tried to add the first controller to the application, but I can't get the new controller to load.
I used:
rails generate controller CollectedData new --no-test-framework
And got back:
create app/controllers/collected_data_controller.rb
route get "collected_data/new"
invoke erb
create app/views/collected_data
create app/views/collected_data/new.html.erb
invoke helper
create app/helpers/collected_data_helper.rb
invoke assets
invoke coffee
create app/assets/javascripts/collected_data.js.coffee
invoke scss
create app/assets/stylesheets/collected_data.css.scss
And I also ran rake routes and got this:
collected_data_new GET /collected_data/new(.:format) collected_data#new
But whenever I load http://localhost:3000/collected_data/new in my browser, I get:
Not Found: /collected_data/new
Here is the content of my routes.rb file:
Crows::Application.routes.draw do
get "collected_data/new"
# The priority is based upon order of creation:
# first created -> highest priority.
# Sample of regular route:
# match 'products/:id' => 'catalog#view'
# Keep in mind you can assign values other than :controller and :action
# Sample of named route:
# match 'products/:id/purchase' => 'catalog#purchase', :as => :purchase
# This route can be invoked with purchase_url(:id => product.id)
# Sample resource route (maps HTTP verbs to controller actions automatically):
# resources :products
# Sample resource route with options:
# resources :products do
# member do
# get 'short'
# post 'toggle'
# end
#
# collection do
# get 'sold'
# end
# end
# Sample resource route with sub-resources:
# resources :products do
# resources :comments, :sales
# resource :seller
# end
# Sample resource route with more complex sub-resources
# resources :products do
# resources :comments
# resources :sales do
# get 'recent', :on => :collection
# end
# end
# Sample resource route within a namespace:
# namespace :admin do
# # Directs /admin/products/* to Admin::ProductsController
# # (app/controllers/admin/products_controller.rb)
# resources :products
# end
# You can have the root of your site routed with "root"
# just remember to delete public/index.html.
# root :to => 'welcome#index'
# See how all your routes lay out with "rake routes"
# This is a legacy wild controller route that's not recommended for RESTful applications.
# Note: This route will make all actions in every controller accessible via GET requests.
# match ':controller(/:action(/:id))(.:format)'
end
Procfile contents:
redis: redis-server config/redis/redis.conf
clock: bundle exec rake resque:scheduler --trace
cp_resp_poller: bundle exec rake environment resque:work QUEUE=cp_resp_poller
cp_req_sender: bundle exec rake environment resque:work QUEUE=cp_req_sender --trace
server: rails server
I have also tried restarting WEBrick.
Update: I noticed that Webrick is returning HTTP 404 when requesting this controller (or any new controller that I've tried to add, for that matter).
FIXED: Please see my own answer to this question.

FIXED: I fixed this by changing my config.ru file.
The original contents were:
require ::File.expand_path('../config/environment', __FILE__)
run Crows::Application
require 'resque/server'
run Rack::URLMap.new "/resque" => Resque::Server.new
I fixed the controller not found issue by commenting out the last two lines above related to Resque. Those last two lines were added early on while developing the workflow/backend part of my application and enable a web-based admin interface for Resque.
Update: I now have my Rails app (plus its web controllers, etc) and the Resque web admin interface running side-by-side. I got this working by removing those last two lines from the config.ru file and adding the following lines to my routes.rb file:
resources :resque_web_admin
mount Resque::Server, :at => "/resque"

Related

Devise setup errors

I'm having issues setting up devise in a fresh rails app. I've just created a fresh Rails app, added the devise gem and ran bundle install. Now when I try to run rails generate devise:install I get this error:
/Users/Bobo/.rvm/gems/ruby-2.2.4/gems/actionpack-4.2.3/lib/action_dispatch/routing/route_set.rb:557:in `add_route': Invalid route name, already in use: 'new_user_session'
You may have defined two routes with the same name using the `:as` option, or you may be overriding a route already defined by a resource with the same naming.
I'm confused, I haven't even touched my routes file yet, when I run rake:routes it says: You don't have any routes defined!
Why am I getting this error? It is literally the first step I took..
Edit: my routes file
Rails.application.routes.draw do
# The priority is based upon order of creation: first created -> highest priority.
# See how all your routes lay out with "rake routes".
# You can have the root of your site routed with "root"
# root 'welcome#index'
# Example of regular route:
# get 'products/:id' => 'catalog#view'
# Example of named route that can be invoked with purchase_url(id: product.id)
# get 'products/:id/purchase' => 'catalog#purchase', as: :purchase
# Example resource route (maps HTTP verbs to controller actions automatically):
# resources :products
# Example resource route with options:
# resources :products do
# member do
# get 'short'
# post 'toggle'
# end
#
# collection do
# get 'sold'
# end
# end
# Example resource route with sub-resources:
# resources :products do
# resources :comments, :sales
# resource :seller
# end
# Example resource route with more complex sub-resources:
# resources :products do
# resources :comments
# resources :sales do
# get 'recent', on: :collection
# end
# end
# Example resource route with concerns:
# concern :toggleable do
# post 'toggle'
# end
# resources :posts, concerns: :toggleable
# resources :photos, concerns: :toggleable
# Example resource route within a namespace:
# namespace :admin do
# # Directs /admin/products/* to Admin::ProductsController
# # (app/controllers/admin/products_controller.rb)
# resources :products
# end
end
You don't have any routes defined, they are all commented out. Uncomment the root route e.g. change
# root 'welcome#index'
root 'welcome#index'
I did a fresh install in a new directory, no more problems.
Change your routes. As mentioned above set the root
Make sure you have a controller called WELCOME with template and action 'index'
Then root => welcome#index will point to that action.
Run
rails generate devise:install
Run
rails g devise Model
This will automatically add routes for your Model to the routes.rb file
Run rake db:migrate OR with rails5 rails db:migrate
Than you have create a Model with Devise.

No route matches [GET] "/app1"

Rails.root: /home/chris/rails_projects/app1
I get the message about routes. my routes.rb files show:
I do not know how to approach this at all. I am using Ubuntu 12.04 in Virtualbox.
Rails.application.routes.draw do
# The priority is based upon order of creation: first created -> highest priority.
# See how all your routes lay out with "rake routes".
# You can have the root of your site routed with "root"
# root 'welcome#index'
# Example of regular route:
# get 'products/:id' => 'catalog#view'
# Example of named route that can be invoked with purchase_url(id: product.id)
# get 'products/:id/purchase' => 'catalog#purchase', as: :purchase
# Example resource route (maps HTTP verbs to controller actions automatically):
# resources :products
# Example resource route with options:
# resources :products do
# member do
# get 'short'
# post 'toggle'
# end
#
# collection do
# get 'sold'
# end
# end
# Example resource route with sub-resources:
# resources :products do
# resources :comments, :sales
# resource :seller
# end
# Example resource route with more complex sub-resources:
# resources :products do
# resources :comments
# resources :sales do
# get 'recent', on: :collection
# end
# end
# Example resource route with concerns:
# concern :toggleable do
# post 'toggle'
# end
# resources :posts, concerns: :toggleable
# resources :photos, concerns: :toggleable
# Example resource route within a namespace:
# namespace :admin do
# # Directs /admin/products/* to Admin::ProductsController
# # (app/controllers/admin/products_controller.rb)
# resources :products
# end
end
You need to create a controller and a view to direct your app somewhere.
Then you need to route your app to that action by editing your routes file. For example you can uncomment the line:
root 'welcome#index'
This will then mean that when you navigate to the root of your site at the root directory (/app1) will go to the welcome controller, to its index action.
To learn more, have a look at: http://guides.rubyonrails.org/routing.html#using-root
Routes
You need to define the routes your application will use, as follows:
#config/routes.rb
root 'application#index'
This will direct any of the "homepage" traffic to the following controller/action:
#app/controllers/application_controller.rb
Class ApplicationController < ActionController::Base
def index
end
end
--
Routing in Rails
You have to remember that Rails is an MVC-centric web application framework
This means every time you wish to access a "url", you need to define the "route" in your routes.rb file. The way to do this is to reference the relevant controller#actions in your routes file - using the resources method:
#config/routes.rb
root "application#index"
resources :controller_1, :controller_2
As Rails is object-orientated, the routes of your application will generally have to reflect the various resources it has inside. Most people try and define routes based on the presumed "flow" of the app - the truth is that you want to construct them around objects, which typically means controller and/or models

Pull out specific view on devise Rails

I'm applying a design for a Rails app, I usually works on PHP, anyway, I'm doing the reset password modifications the problem is I have the views for reset password, but not the email.
I know there is a generate devise:views which pull up even the email, the problem when I do this is this "damage" the current customizations in some part already done in this app by other programmer.
How can I pull up just the reset instructions email view on devise, so I can edit it?
Thanks.. I guess you can be more specific when you generate views of devise using
generate devise:views
Even more specific than
generate devise:views users
Also in the email the link is directed to localhost, for this issue I found this Devise: edit_password_url in password reset email is sending users to url/api/v1/
The problem is I can't make that work in my routes.rb which is here
Consult::Application.routes.draw do
devise_for :users, :controllers => { :registrations => "registrations" }
scope "/admin" do
resources :users, :controller => 'admin/users'
end
resources :players
resources :player_steps
resources :coach_steps
resources :candidates
resource :payment_notifications, :only => :show
#match 'candidates' => 'candidates#index'
match '*a', :to => 'errors#routing'
# The priority is based upon order of creation:
# first created -> highest priority.
# Sample of regular route:
# match 'products/:id' => 'catalog#view'
# Keep in mind you can assign values other than :controller and :action
# Sample of named route:
# match 'products/:id/purchase' => 'catalog#purchase', :as => :purchase
# This route can be invoked with purchase_url(:id => product.id)
# Sample resource route (maps HTTP verbs to controller actions automatically):
# resources :products
# Sample resource route with options:
# resources :products do
# member do
# get 'short'
# post 'toggle'
# end
#
# collection do
# get 'sold'
# end
# end
# Sample resource route with sub-resources:
# resources :products do
# resources :comments, :sales
# resource :seller
# end
# Sample resource route with more complex sub-resources
# resources :products do
# resources :comments
# resources :sales do
# get 'recent', :on => :collection
# end
# end
# Sample resource route within a namespace:
# namespace :admin do
# # Directs /admin/products/* to Admin::ProductsController
# # (app/controllers/admin/products_controller.rb)
# resources :products
# end
# You can have the root of your site routed with "root"
# just remember to delete public/index.html.
#root :to => "devise/sessions#new"
get 'user_type', to: 'home#user_type', as: :choose
devise_scope :user do
root :to => "devise/sessions#new"
end
# See how all your routes lay out with "rake routes"
# This is a legacy wild controller route that's not recommended for RESTful applications.
# Note: This route will make all actions in every controller accessible via GET requests.
# match ':controller(/:action(/:id))(.:format)'
end
How in the name of rails can I make this work so that I can finish this?
Rails has some helpers that can come in useful when you do something wrong, such as
rails generate model Fish
rails g controller Fishes
You can also apply this to destroying the action task if you need too
rails destroy model Fish
rails destroy controller Fishes
You can apply this to anything with rails at the front so:
rails generate devise:views
rails destroy devise:views

Rails devise not working for rails 3.2.1 on localhost

I followed each and every single step for devise to work on my localhost as discussed under this tutorial - http://www.slideshare.net/wleeper/devise-and-rails upto page 8 of 22 and I was expecting the same user login form as mentioned on the page 8 but apart from this, i am getting this error-
If i keep the comments as it is on routes.rb file,
# match ':controller(/:action(/:id))(.:format)'
then this message flags -
And when i uncomment this line it show this error-
Have very less idea about rails, as I am from PHP background, but still trying some practical tests before theory, so let me know what exactly im doing wrong.
My routes.rb file -
Prjmgt::Application.routes.draw do
devise_for :users
# The priority is based upon order of creation:
# first created -> highest priority.
# Sample of regular route:
# match 'products/:id' => 'catalog#view'
# Keep in mind you can assign values other than :controller and :action
# Sample of named route:
# match 'products/:id/purchase' => 'catalog#purchase', :as => :purchase
# This route can be invoked with purchase_url(:id => product.id)
# Sample resource route (maps HTTP verbs to controller actions automatically):
# resources :products
# Sample resource route with options:
# resources :products do
# member do
# get 'short'
# post 'toggle'
# end
#
# collection do
# get 'sold'
# end
# end
# Sample resource route with sub-resources:
# resources :products do
# resources :comments, :sales
# resource :seller
# end
# Sample resource route with more complex sub-resources
# resources :products do
# resources :comments
# resources :sales do
# get 'recent', :on => :collection
# end
# end
# Sample resource route within a namespace:
# namespace :admin do
# # Directs /admin/products/* to Admin::ProductsController
# # (app/controllers/admin/products_controller.rb)
# resources :products
# end
# You can have the root of your site routed with "root"
# just remember to delete public/index.html.
# root :to => 'welcome#index'
root :to => 'home#index'
# See how all your routes lay out with "rake routes"
# This is a legacy wild controller route that's not recommended for RESTful applications.
# Note: This route will make all actions in every controller accessible via GET requests.
match ':controller(/:action(/:id))(.:format)'
end
Just run a rake routes command and find this -
You are accessing the wrong url.. you should try it with
http://application/users/login
you dont have the right root url like
root :to => redirect("/users/login")

Unable to parse the csv file in ruby and rails

I am a newbie to the ruby on rails platform and I was just trying out couple of example codes.
I was trying to run this example http://goodbadtech.com/2009/05/13/ruby-on-rails-import-csv-data-into-database/
I followed all the instructions but I am getting this error
ActionController::RoutingError (uninitialized constant CsvImportController):
Please help me to bash this error.
Here is my Routes.rb
Imports::Application.routes.draw do
# The priority is based upon order of creation:
# first created -> highest priority.
get "csv_imports/csv_view"
#map.resources :imports
#map.import_proc '/import/proc/:id', :controller => "imports", :action => "proc_csv"
# Sample of regular route:
# match 'products/:id' => 'catalog#view'
# Keep in mind you can assign values other than :controller and :action
# Sample of named route:
# match 'products/:id/purchase' => 'catalog#purchase', :as => :purchase
# This route can be invoked with purchase_url(:id => product.id)
resources :imports
import_proc '/import/proc/:id', :controller => "csv_imports", :action => "pro_csv"
end
# Sample resource route (maps HTTP verbs to controller actions automatically):
# resources :products
# Sample resource route with options:
# resources :products do
# member do
# get 'short'
# post 'toggle'
# end
#
# collection do
# get 'sold'
# end
# end
# Sample resource route with sub-resources:
# resources :products do
# resources :comments, :sales
# resource :seller
# end
# Sample resource route with more complex sub-resources
# resources :products do
# resources :comments
# resources :sales do
# get 'recent', :on => :collection
# end
# end
# Sample resource route within a namespace:
# namespace :admin do
# # Directs /admin/products/* to Admin::ProductsController
# # (app/controllers/admin/products_controller.rb)
# resources :products
# end
# You can have the root of your site routed with "root"
# just remember to delete public/index.html.
# root :to => 'csv_imports#csv_view'
# See how all your routes lay out with "rake routes"
# This is a legacy wild controller route that's not recommended for RESTful applications.
# Note: This route will make all actions in every controller accessible via GET requests.
# match ':controller(/:action(/:id(.:format)))'
Your file, containing the class CsvImportsController should be named csv_imports_controller. I believe that is the error. In your routing, you should have
resources :csv_imports
[EDIT] On the other hand, if your controller is named ImportsController, placed in imports_controller.rb, then inside your routing, you should have
resources :imports
Rails automatically tries to tie things together based on the names. This is what makes things easy if you follow them correctly. So resources :imports will assume there is a controller called ImportsController, which can be found in app/controllers/imports_controller.rb. It is best practice to call the relevant model Import, to be found in app/models/import.rb.
Hope this helps.
(also note that the blog-post you mention is for Rails 2 and not Rails 3)

Resources