0
0
Ruby on Railsframework~30 mins

Why structure conventions matter in Ruby on Rails - See It in Action

Choose your learning style9 modes available
Why structure conventions matter
📖 Scenario: You are building a simple blog application using Ruby on Rails. Rails has a specific folder and file structure that helps the framework find your code easily and keeps your project organized.Following these conventions makes your app work smoothly and helps other developers understand your code quickly.
🎯 Goal: Create a basic Rails model, controller, and view following Rails naming and folder conventions to display a list of blog posts.
📋 What You'll Learn
Create a model named Post in the correct folder
Create a controller named PostsController in the correct folder
Add an action index in PostsController
Create a view file index.html.erb in the correct folder
Use Rails conventions for naming and file placement
💡 Why This Matters
🌍 Real World
Rails conventions make it easy to build web apps quickly and maintain them as they grow.
💼 Career
Understanding Rails structure is essential for working on Rails projects in web development jobs.
Progress0 / 4 steps
1
Create the Post model
Create a Ruby class named Post inside the file app/models/post.rb. The class should inherit from ApplicationRecord.
Ruby on Rails
Need a hint?

The model class goes inside app/models/ and inherits from ApplicationRecord.

2
Create the PostsController with index action
Create a controller class named PostsController inside the file app/controllers/posts_controller.rb. Add a method index that will be used to show all posts.
Ruby on Rails
Need a hint?

Controllers go inside app/controllers/ and inherit from ApplicationController.

Define the index method inside the controller.

3
Create the index view for posts
Create a view file named index.html.erb inside the folder app/views/posts/. Add a simple HTML heading <h1>All Posts</h1> inside this file.
Ruby on Rails
Need a hint?

Views are placed in app/views/ inside a folder named after the controller (pluralized).

The file name matches the action name with extension .html.erb.

4
Add route for posts index
In the file config/routes.rb, add a route that maps GET /posts to the index action of PostsController using resources :posts, only: [:index].
Ruby on Rails
Need a hint?

Routes are defined in config/routes.rb. Use resources :posts, only: [:index] to create the route.