0
0
Ruby on Railsframework~5 mins

App folder organization in Ruby on Rails

Choose your learning style9 modes available
Introduction

The app folder in a Rails project keeps your code organized by type. It helps you find and manage your files easily.

When starting a new Rails project to know where to put your code.
When adding new features like models, views, or controllers.
When debugging to quickly locate the relevant files.
When collaborating with others to follow a common structure.
When maintaining or updating your app to keep it clean.
Syntax
Ruby on Rails
app/
  controllers/
  models/
  views/
  helpers/
  assets/
  channels/
  jobs/
  mailers/

controllers/ holds code that responds to user actions.

models/ contains data and business logic.

Examples
This file handles user-related actions like signup or login.
Ruby on Rails
app/controllers/users_controller.rb
This file defines the User data and rules.
Ruby on Rails
app/models/user.rb
This file shows the list of users in the browser.
Ruby on Rails
app/views/users/index.html.erb
Sample Program

This example shows a simple controller in the controllers/ folder that responds with a plain text greeting. The route connects the URL /hello to this controller action.

Ruby on Rails
# File: app/controllers/greetings_controller.rb
class GreetingsController < ApplicationController
  def hello
    render plain: "Hello, Rails!"
  end
end

# File: config/routes.rb
Rails.application.routes.draw do
  get '/hello', to: 'greetings#hello'
end
OutputSuccess
Important Notes

Keep your code in the right folders to follow Rails conventions.

Use subfolders inside controllers/ or views/ for large apps to stay organized.

Summary

The app/ folder organizes your Rails code by type.

Controllers handle requests, models manage data, and views display content.

Following this structure makes your app easier to build and maintain.