Rails Simple Counter App - Ruby-on-rails Typing CST Test
Loading…
Rails Simple Counter App — Ruby-on-rails Code
Demonstrates a simple Rails controller and view for a counter using in-memory state (for demonstration purposes).
# 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
endRuby-on-rails Language Guide
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.
Primary Use Cases
- ▸Building full-stack web applications
- ▸Developing RESTful APIs
- ▸Rapid MVP and startup projects
- ▸Database-driven enterprise apps
- ▸E-commerce platforms and SaaS products
Notable Features
- ▸Convention over configuration
- ▸MVC architecture
- ▸Active Record ORM
- ▸Built-in testing framework
- ▸RESTful routing and resource management
Origin & Creator
Ruby on Rails was created by David Heinemeier Hansson and first released in 2004.
Industrial Note
Rails is ideal for startups and projects requiring fast prototyping, robust full-stack development, and applications with database-driven backends.