Unable to parse the csv file in ruby and rails - ruby-on-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)

Related

Undefined local variable or method `‘welcome' for #<ActionDispatch::Routing::Mapper:0x007fd561953218>

I follow the ruby on rails tutorial guide but I keep having the error as stated in my title. I have totally no idea why does it appear.
routes.rb
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 ‘application#hello’
# 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
application_controller.rb
class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception
def hello
render text: “Hello”
end
end
Please advise me on this because I have been trying around and even reinstalling everything again based on the Ruby on Rails tutorial.
The problem is in this line root ‘application#hello’.
It looks like your system uses wrong symbol for single quotation mark (the one you are using is 8216 unicode character and you should be using the 39 one). Also in your controller render text: “Hello” would throw an error.
The lines should be root 'application#hello' and render text: "Hello"
This may be changed in your system keyboard settings. Cannot be more accurate since I don't know what system are you using but the issue is definitely in wrong symbols for single\double quotations.

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

Rails - how to make "Hello, Rails" appear on a web browser

I am coding my first Rails app by following the tutorial on
http://guides.rubyonrails.org/
So far good.. the app (called 'blog') is created and I edited the view
html.erb file by typing <h1>Hello, Rails</1>
Now I have to edit the routes.rb file by uncommenting the line
containing 'root'. The result should be:
root "welcome#index"
but when I type http://localhost:3000
I get the following error message:
Oops! Google Chrome could not connect to localhost:3000
Did you mean: localhost3000.­org
Here are the codes in the routes.rb file. Any help will be
appreciated!
Blog::Application.routes.draw do
get "welcome/index"
# 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 probably didn't start the server.
type rails s to start a server on localhost:3000
you need to do the following:
rails g controller Welcome index
rails s
then you have access to your localhost:3000 on your browser

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")

Resources