John Malkovitz
John Malkovitz

Reputation: 55

Ruby on Rails Tutorial, Michael Hartl Chapter 2

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

Upvotes: 0

Views: 243

Answers (2)

steve klein
steve klein

Reputation: 2629

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.

Upvotes: 2

bspencer
bspencer

Reputation: 1

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

Upvotes: 0

Related Questions