Introduction
Structure conventions help keep your code organized and easy to understand. They make teamwork smoother and save time when building apps.
Structure conventions help keep your code organized and easy to understand. They make teamwork smoother and save time when building apps.
app/ controllers/ models/ views/ config/ db/ lib/ test/ public/
controllers, models, and views inside app/.app/controllers/users_controller.rb app/models/user.rb app/views/users/index.html.erb
config/routes.rb
db/migrate/20240101010101_create_users.rbThis simple Rails app shows how following structure conventions lets Rails find the controller and route to display a greeting.
# File: app/controllers/welcome_controller.rb class WelcomeController < ApplicationController def index render plain: "Hello, Rails!" end end # File: config/routes.rb Rails.application.routes.draw do root "welcome#index" end
Always place files in the correct folders to avoid errors.
Using Rails conventions means less setup and more focus on your app's features.
Structure conventions keep your Rails app organized and easy to navigate.
They help Rails automatically find and connect parts of your app.
Following conventions saves time and reduces mistakes.
app/controllers folder.app/models, views in app/views, and helpers in app/helpers.app/models instead of app/controllers?
class ProductsController < ApplicationController
def index
@products = Product.all
end
endapp/controllers folder by convention.app/models, Rails won't find it as a controller and will raise a routing or missing controller error.app/models/user.rb to app/controllers/user.rb. What problem will this cause?app/models for Rails to load them as data objects.app/controllers makes Rails treat it as a controller, causing errors because the code does not match controller expectations.app/views inside a folder named after the controller, here books.app/views/books to match the BooksController and render pages properly.Pass the test and this pattern moves unknown