0
0
Ruby on Railsframework~30 mins

App folder organization in Ruby on Rails - Mini Project: Build & Apply

Choose your learning style9 modes available
Organize a Rails App Folder
📖 Scenario: You are starting a new Ruby on Rails project for a simple blog website. To keep your project clean and easy to understand, you need to organize the main folders and files inside the app directory properly.
🎯 Goal: Learn how to create the basic folder structure inside the app folder of a Rails project, including models, views, and controllers folders, and add a simple Ruby file inside the models folder.
📋 What You'll Learn
Create the main folders inside the app directory: models, views, and controllers
Add a Ruby file named post.rb inside the models folder
Inside post.rb, define a class Post that inherits from ApplicationRecord
Create a configuration variable MAX_POSTS inside post.rb and set it to 10
Add a method self.max_posts inside the Post class that returns the value of MAX_POSTS
💡 Why This Matters
🌍 Real World
Organizing the app folder correctly is essential for Rails projects to keep code clean and maintainable. It helps developers find files quickly and follow Rails conventions.
💼 Career
Understanding Rails folder structure and how to create models, views, and controllers is a fundamental skill for Rails developers working on web applications.
Progress0 / 4 steps
1
Create the main folders inside app
Create three folders inside the app directory named models, views, and controllers.
Ruby on Rails
Need a hint?

In your Rails project folder, create three new folders named exactly models, views, and controllers inside the app folder.

2
Add post.rb file with Post class
Inside the models folder, create a file named post.rb. In this file, define a class called Post that inherits from ApplicationRecord.
Ruby on Rails
Need a hint?

Define a Ruby class named Post that inherits from ApplicationRecord inside post.rb.

3
Add a configuration variable MAX_POSTS
Inside the Post class in post.rb, add a constant variable named MAX_POSTS and set it to 10.
Ruby on Rails
Need a hint?

Inside the Post class, write MAX_POSTS = 10 to create a constant.

4
Add a class method self.max_posts returning MAX_POSTS
Inside the Post class in post.rb, add a class method named self.max_posts that returns the value of the constant MAX_POSTS.
Ruby on Rails
Need a hint?

Define a class method self.max_posts that returns MAX_POSTS inside the Post class.