Learn RUBY-ON-RAILS with Real Code Examples
Updated Nov 25, 2025
Code Sample Descriptions
1
Rails Simple Counter App
# config/routes.rb
Rails.application.routes.draw do
get '/counter', to: 'counter#show'
post '/counter/increment', to: 'counter#increment'
post '/counter/decrement', to: 'counter#decrement'
post '/counter/reset', to: 'counter#reset'
end
# app/controllers/counter_controller.rb
class CounterController < ApplicationController
@@count = 0
def show
render html: "<h2>Counter: #{@@count}</h2>".html_safe
end
def increment
@@count += 1
redirect_to '/counter'
end
def decrement
@@count -= 1
redirect_to '/counter'
end
def reset
@@count = 0
redirect_to '/counter'
end
end
Demonstrates a simple Rails controller and view for a counter using in-memory state (for demonstration purposes).
2
Rails Hello World API
# config/routes.rb
Rails.application.routes.draw do
get '/hello', to: 'hello#index'
end
# app/controllers/hello_controller.rb
class HelloController < ApplicationController
def index
render json: { message: 'Hello, Rails!' }
end
end
A minimal Rails API controller returning Hello World.
3
Rails Query Parameter Example
# config/routes.rb
Rails.application.routes.draw do
get '/greet', to: 'greet#index'
end
# app/controllers/greet_controller.rb
class GreetController < ApplicationController
def index
name = params[:name] || 'Guest'
render json: { message: "Hello, #{name}" }
end
end
Demonstrates using query parameters in Rails controller.
4
Rails JSON Response Example
# config/routes.rb
Rails.application.routes.draw do
get '/json', to: 'json#index'
end
# app/controllers/json_controller.rb
class JsonController < ApplicationController
def index
render json: { message: 'Hello, Rails JSON' }
end
end
Return JSON response from Rails controller.
5
Rails POST Form Example
# config/routes.rb
Rails.application.routes.draw do
post '/form', to: 'form#submit'
end
# app/controllers/form_controller.rb
class FormController < ApplicationController
def submit
name = params[:name]
render json: { message: "Hello, #{name}" }
end
end
Handle POST requests with form parameters in Rails.
6
Rails Path Parameter Example
# config/routes.rb
Rails.application.routes.draw do
get '/user/:id', to: 'user#show'
end
# app/controllers/user_controller.rb
class UserController < ApplicationController
def show
id = params[:id]
render json: { user_id: id }
end
end
Use path parameters in Rails routes.
7
Rails Error Handling Example
# config/routes.rb
Rails.application.routes.draw do
get '/error', to: 'error#index'
end
# app/controllers/error_controller.rb
class ErrorController < ApplicationController
def index
raise 'This is an error'
rescue => e
render json: { error: e.message }, status: 400
end
end
Demonstrates basic exception handling in Rails controller.
8
Rails Redirect Example
# config/routes.rb
Rails.application.routes.draw do
get '/redirect', to: 'redirect#index'
get '/counter', to: 'counter#show'
end
# app/controllers/redirect_controller.rb
class RedirectController < ApplicationController
def index
redirect_to '/counter'
end
end
Redirect request to another route in Rails controller.
9
Rails Multiple Routes Example
# config/routes.rb
Rails.application.routes.draw do
get '/', to: 'home#index'
get '/about', to: 'home#about'
end
# app/controllers/home_controller.rb
class HomeController < ApplicationController
def index
render plain: 'Home'
end
def about
render plain: 'About'
end
end
Handle multiple routes in a single Rails application.
10
Rails Service Object Example
# app/services/counter_service.rb
class CounterService
@@count = 0
def self.get_count
@@count
end
def self.increment
@@count += 1
end
def self.reset
@@count = 0
end
end
# app/controllers/counter_controller.rb
class CounterController < ApplicationController
def show
render json: { count: CounterService.get_count }
end
def increment
CounterService.increment
redirect_to '/counter'
end
def reset
CounterService.reset
redirect_to '/counter'
end
end
Use a service object to manage counter state.