Learn Ruby-on-rails - 10 Code Examples & CST Typing Practice Test
Ruby on Rails (Rails) is a full-stack, server-side web application framework written in Ruby. It emphasizes convention over configuration, DRY (Don't Repeat Yourself) principles, and rapid development.
View all 10 Ruby-on-rails code examples →
Learn RUBY-ON-RAILS with Real Code Examples
Updated Nov 25, 2025
Code Sample Descriptions
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).
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.
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.
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.
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.
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.
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.
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.
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.
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.
Frequently Asked Questions about Ruby-on-rails
What is Ruby-on-rails?
Ruby on Rails (Rails) is a full-stack, server-side web application framework written in Ruby. It emphasizes convention over configuration, DRY (Don't Repeat Yourself) principles, and rapid development.
What are the primary use cases for Ruby-on-rails?
Building full-stack web applications. Developing RESTful APIs. Rapid MVP and startup projects. Database-driven enterprise apps. E-commerce platforms and SaaS products
What are the strengths of Ruby-on-rails?
Rapid development with sensible defaults. Large ecosystem of gems (libraries). Strong community and documentation. Built-in security features. Integrated testing support
What are the limitations of Ruby-on-rails?
Performance may lag behind lighter frameworks. Monolithic by default, less flexible for microservices. Learning curve for Rails conventions. Can be overkill for very small apps. Upgrading major versions can require significant refactoring
How can I practice Ruby-on-rails typing speed?
CodeSpeedTest offers 10+ real Ruby-on-rails code examples for typing practice. You can measure your WPM, track accuracy, and improve your coding speed with guided exercises.