In the toy_app exercise https://www.railstutorial.org/book/toy_app I'm having a problem changing the default message displayed when running the application from the generic : "Welcome aboard, you're riding ruby on rails" to "hello, world!" by routing the page to the hello method in the application_controller file.
This is what i have in controller/application_controller:
Rails.application.routes.draw do
# Prevent CSFR attacks by raising an exception.
# For API's, you may want to use :null_session instead
protect_from_forgery with: :exception
def hello
render text: "hello, world!"
end
end
and in locales/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
In application_controller.rb, replace Rails.application.routes.draw do with class ApplicationController < ActionController::Base. It looks like this line was inadvertently replaced with a copy of the first line from routes.rb.
Assuming you have a template like app/views/layouts/application.html.erb defined, you should be good to go.
You should only put routes in your route file.
You don't want to put methods in there.
Try storing your methods in the controlle
Related
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.
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
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
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")
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)