First, let's create a new Rails application. You can do this using the following command in your terminal:
rails new my_blog
This will create a new Rails application named "my_blog". Now, navigate into the application directory:
cd my_blog
Let's start a Rails server to preview our application:
rails server
Open your web browser and go to http://localhost:3000. You should see the default Rails welcome page.
Now, let's create the model for our blog posts. In your terminal, run:
rails generate model Post title:string body:text
This will create a new model called "Post" with two attributes: title
(string) and body
(text). We'll use this model to store our blog posts.
Now, let's create a migration to add these attributes to our database. Run this command:
rails db:migrate
This will create a migration file and update your database with the new table and attributes.
Let's create a controller for managing our blog posts:
rails generate controller Posts index show new create edit update destroy
This will generate a new controller called "PostsController" with the following actions: index
, show
, new
, create
, edit
, update
, and destroy
. These actions will handle displaying all posts, showing a single post, creating a new post, editing an existing post, updating a post, and deleting a post, respectively.
We also need to create views for these actions. These views will be rendered by the controller and will display the data to the user.
© 2023 Your Name
We've set up the model, controller, and views, but now we need to connect them. Let's start with the index
action in the PostsController
. Open the file app/controllers/posts_controller.rb
and add the following code:
class PostsController < ApplicationController def index @posts = Post.all end end
This code fetches all blog posts from the database and assigns them to the @posts
variable. This variable will be available in the corresponding view, app/views/posts/index.html.erb
. This file will be responsible for rendering the list of blog posts.
Now, let's create the show
view for displaying a single blog post. Open the file app/views/posts/show.html.erb
and add the following code:
<%= @post.title %>
<%= @post.body %>
This code will display the title and body of the blog post.
For creating new blog posts, we need the new
view for creating a form to enter the post details and the create
view for handling the form submission.
# app/views/posts/new.html.erbNew Post
<%= form_with model: @post, url: posts_path do |f| %><%= f.label :title %> <%= f.text_field :title %><%= f.label :body %> <%= f.text_area :body %><%= f.submit "Create Post" %> <% end %> # app/views/posts/create.html.erb # This view is not required as the create action will redirect to the show view
The new
view uses the form_with
helper to create a form with input fields for title and body. The create
view doesn't need any code as it will be handled by the controller and redirect to the show
view.
© 2023 Your Name
Similar to the new
and create
views, we need the edit
and update
views for editing existing blog posts. The edit
view will display the form to edit the post, and the update
view will handle the submission.
# app/views/posts/edit.html.erbEdit Post
<%= form_with model: @post, url: post_path(@post) do |f| %><%= f.label :title %> <%= f.text_field :title %><%= f.label :body %> <%= f.text_area :body %><%= f.submit "Update Post" %> <% end %> # app/views/posts/update.html.erb # This view is not required as the update action will redirect to the show view
The edit
view uses form_with
with the existing post object and the update
action in the controller to handle the submission.
The destroy
view is used to delete a post. It doesn't require any HTML code as it will be handled by the controller.
# app/views/posts/destroy.html.erb # This view is not required as the destroy action will redirect to the index view
Now, you can start creating posts in your browser and testing the functionality you've added. You can navigate to the index page, create new posts, view single posts, edit them, and delete them. Make sure everything works as expected.
You can add more features to your blogging platform, such as:
By adding these features, you can create a more comprehensive and user-friendly blogging platform.
© 2023 Your Name