user5892985
user5892985

Reputation:

Ruby On Rails Home Page Custom Default

I am trying to set my custom homepage as my default when I open my localhost ruby on rails . I followed about two answers to questions on here and this tutorial http://blog.teamtreehouse.com/static-pages-ruby-rails . I am also trying to write my code in notepad ++ so that it could show on my web browser(Google Chrome). If someone could help me with the tutorial I am trying to follow on team tree house or give me another answer , I would really appreciate it . Thanks

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#code'

# 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: 1054

Answers (1)

Richard Peck
Richard Peck

Reputation: 76774

You should do the following:

#config/routes.rb
root "welcome#code"

#app/controllers/welcome_controller.rb
class WelcomeController < ApplicationController
  def code
  end
end

#app/views/welcome/code.html.erb
Hello world!

This will give you the ability to browse to localhost:3000 (lvh.me:3000) and have the contents of welcome#code displayed.


As an extension (since you're a beginner), any "random" methods in your controllers should be put into ApplicationController. This way, you negate having to add an unnecessary WelcomeController...

#config/routes.rb
root "application#welcome"

#app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
  def welcome
  end
end

#app/views/application/welcome.html.erb
Hello world.

Upvotes: 1

Related Questions