{"id":4213,"date":"2023-11-04T23:14:09","date_gmt":"2023-11-04T23:14:09","guid":{"rendered":"http:\/\/localhost:10003\/how-to-build-a-social-media-app-with-ruby-on-rails\/"},"modified":"2023-11-05T05:47:55","modified_gmt":"2023-11-05T05:47:55","slug":"how-to-build-a-social-media-app-with-ruby-on-rails","status":"publish","type":"post","link":"http:\/\/localhost:10003\/how-to-build-a-social-media-app-with-ruby-on-rails\/","title":{"rendered":"How to Build a Social Media App with Ruby on Rails"},"content":{"rendered":"
In this tutorial, we will walk through the process of building a social media app using Ruby on Rails. We will cover the basics of setting up a Rails application, creating models and associations, implementing user registration and authentication, and building the core functionality of a social media app, including posting and commenting.<\/p>\n
By the end of this tutorial, you will have a working social media app that allows users to create accounts, make posts, and interact with other users’ posts through comments.<\/p>\n
Before we begin, make sure you have the following installed on your machine:<\/p>\n
Let’s start by creating a new Rails application. Open your terminal and run the following command:<\/p>\n
rails new social_media_app\n<\/code><\/pre>\nThis will generate a new Rails application in a directory called social_media_app<\/code>. Change into the application directory by running:<\/p>\ncd social_media_app\n<\/code><\/pre>\nStep 2: Setting Up the Database<\/h2>\n
Next, we need to set up the database for our application. Open the config\/database.yml<\/code> file and configure the database connection settings based on your local setup.<\/p>\ndefault: &default\n adapter: postgresql\n encoding: unicode\n username: YOUR_USERNAME\n password: YOUR_PASSWORD\n host: localhost\n port: 5432\n\ndevelopment:\n <<: *default\n database: social_media_app_development\n\ntest:\n <<: *default\n database: social_media_app_test\n\nproduction:\n <<: *default\n database: social_media_app_production\n<\/code><\/pre>\nMake sure to replace YOUR_USERNAME<\/code> and YOUR_PASSWORD<\/code> with your PostgreSQL username and password.<\/p>\nRun the following command to create the development and test databases:<\/p>\n
rails db:create\n<\/code><\/pre>\nStep 3: Generating User Model and Authentication<\/h2>\n
We will be using the popular Devise<\/code> gem for user authentication. Add Devise<\/code> to your Gemfile<\/code> by uncommenting the line:<\/p>\ngem 'devise'\n<\/code><\/pre>\nSave the file and run the following command to install the gem:<\/p>\n
bundle install\n<\/code><\/pre>\nNext, generate the Devise configuration files and the User model by running the following command:<\/p>\n
rails generate devise:install\n<\/code><\/pre>\nThis will generate the initial configuration files and the User model.<\/p>\n
Migrate the database to create the necessary tables for Devise by running:<\/p>\n
rails db:migrate\n<\/code><\/pre>\nNext, generate the views and controller for User registration by running:<\/p>\n
rails generate devise:views\nrails generate devise:controllers users\n<\/code><\/pre>\nStep 4: Creating Associations<\/h2>\n
Now that we have the User model set up, let’s create the other models and associations needed for our social media app.<\/p>\n
Generate the Post model by running the following command:<\/p>\n
rails generate model Post content:text user:references\n<\/code><\/pre>\nThe content<\/code> attribute will store the content of the user’s post and the user_id<\/code> will be used to associate the post with the user who created it.<\/p>\nMigrate the database to create the posts table by running:<\/p>\n
rails db:migrate\n<\/code><\/pre>\nNext, generate the Comment model by running the following command:<\/p>\n
rails generate model Comment content:text user:references post:references\n<\/code><\/pre>\nThe content<\/code> attribute will store the content of the user’s comment, the user_id<\/code> will be used to associate the comment with the user who created it, and the post_id<\/code> will be used to associate the comment with the post it belongs to.<\/p>\nMigrate the database to create the comments table by running:<\/p>\n
rails db:migrate\n<\/code><\/pre>\nDefine the associations in the models as follows:<\/p>\n
# app\/models\/post.rb\nclass Post < ApplicationRecord\n belongs_to :user\n has_many :comments\nend\n\n# app\/models\/comment.rb\nclass Comment < ApplicationRecord\n belongs_to :user\n belongs_to :post\nend\n\n# app\/models\/user.rb\nclass User < ApplicationRecord\n # ...\n has_many :posts\n has_many :comments\nend\n<\/code><\/pre>\nStep 5: Generating Controllers and Views<\/h2>\n
Now that our models and associations are set up, let’s generate the controllers and views for our posts and comments.<\/p>\n
Generate the Posts controller by running the following command:<\/p>\n
rails generate controller Posts\n<\/code><\/pre>\nNow, open the app\/controllers\/posts_controller.rb<\/code> file and define the actions for creating, showing, and deleting posts:<\/p>\nclass PostsController < ApplicationController\n before_action :authenticate_user!\n\n def index\n @posts = Post.all.order(created_at: :desc)\n end\n\n def new\n @post = current_user.posts.build\n end\n\n def create\n @post = current_user.posts.build(post_params)\n if @post.save\n redirect_to posts_path, notice: 'Post was successfully created.'\n else\n render :new\n end\n end\n\n def show\n @post = Post.find(params[:id])\n @comment = Comment.new\n end\n\n def destroy\n @post = Post.find(params[:id])\n @post.destroy\n redirect_to posts_path, notice: 'Post was successfully destroyed.'\n end\n\n private\n\n def post_params\n params.require(:post).permit(:content)\n end\nend\n<\/code><\/pre>\nNext, generate the Comments controller by running the following command:<\/p>\n
rails generate controller Comments\n<\/code><\/pre>\nOpen the app\/controllers\/comments_controller.rb<\/code> file and define the actions for creating and destroying comments:<\/p>\nclass CommentsController < ApplicationController\n before_action :authenticate_user!\n\n def create\n @post = Post.find(params[:post_id])\n @comment = @post.comments.build(comment_params)\n @comment.user = current_user\n if @comment.save\n redirect_to post_path(@post), notice: 'Comment was successfully created.'\n else\n redirect_to post_path(@post), alert: 'Comment could not be created.'\n end\n end\n\n def destroy\n @comment = Comment.find(params[:id])\n @comment.destroy\n redirect_to post_path(@comment.post), notice: 'Comment was successfully destroyed.'\n end\n\n private\n\n def comment_params\n params.require(:comment).permit(:content)\n end\nend\n<\/code><\/pre>\nStep 6: Creating the Views<\/h2>\n
Now that our controllers are set up, let’s generate the views for our posts and comments.<\/p>\n
Create a new file called index.html.erb<\/code> in the app\/views\/posts<\/code> directory and add the following code:<\/p>\n<h1>Posts<\/h1>\n\n<%= link_to 'New Post', new_post_path %>\n\n<% @posts.each do |post| %>\n <div>\n <h2><%= post.user.email %><\/h2>\n <p><%= post.content %><\/p>\n\n <%= link_to 'View', post_path(post) %>\n\n <% if post.user == current_user %>\n <%= link_to 'Delete', post_path(post), method: :delete, data: { confirm: 'Are you sure?' } %>\n <% end %>\n <\/div>\n<% end %>\n<\/code><\/pre>\nCreate a new file called show.html.erb<\/code> in the app\/views\/posts<\/code> directory and add the following code:<\/p>\n<h1>Post<\/h1>\n\n<h2><%= @post.user.email %><\/h2>\n<p><%= @post.content %><\/p>\n\n<h3>Comments<\/h3>\n\n<% @post.comments.each do |comment| %>\n <div>\n <h4><%= comment.user.email %><\/h4>\n <p><%= comment.content %><\/p>\n\n <% if comment.user == current_user %>\n <%= link_to 'Delete', comment_path(comment), method: :delete, data: { confirm: 'Are you sure?' } %>\n <% end %>\n <\/div>\n<% end %>\n\n<%= form_with(model: [@post, Comment.new]) do |form| %>\n <%= form.text_field :content %>\n <%= form.submit 'Add Comment' %>\n<% end %>\n<\/code><\/pre>\nCreate a new file called new.html.erb<\/code> in the app\/views\/posts<\/code> directory and add the following code:<\/p>\n<h1>New Post<\/h1>\n\n<%= form_with(model: @post) do |form| %>\n <%= form.text_area :content %>\n <%= form.submit 'Create Post' %>\n<% end %>\n<\/code><\/pre>\nCreate a new file called _comment.html.erb<\/code> in the app\/views\/comments<\/code> directory and add the following code:<\/p>\n<p><%= comment.content %><\/p>\n\n<%= link_to 'Delete', comment_path(comment), method: :delete, data: { confirm: 'Are you sure?' } %>\n<\/code><\/pre>\nFinally, modify the app\/views\/layouts\/application.html.erb<\/code> file to include a link to the posts index page:<\/p>\n<!DOCTYPE html>\n<html>\n <head>\n <title>Social Media App<\/title>\n <%= csrf_meta_tags %>\n <%= csp_meta_tag %>\n <%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>\n <%= javascript_pack_tag 'application', 'data-turbolinks-track': 'reload' %>\n <\/head>\n <body>\n <header>\n <%= link_to 'Home', posts_path %>\n <% if user_signed_in? %>\n Logged in as: <%= current_user.email %>\n <%= link_to 'Logout', destroy_user_session_path, method: :delete %>\n <% else %>\n <%= link_to 'Login', new_user_session_path %>\n <% end %>\n <\/header>\n\n <main>\n <%= yield %>\n <\/main>\n <\/body>\n<\/html>\n<\/code><\/pre>\nStep 7: Testing the Application<\/h2>\n
Start the Rails server by running the following command:<\/p>\n
rails server\n<\/code><\/pre>\nOpen your web browser and visit `http:\/\/localhost:3000`. You should see the posts index page. If you try to create a new post or comment without logging in, you will be redirected to the login page. Sign up for a new account and try creating posts and comments.<\/p>\n
Congratulations! You have successfully built a social media app with Ruby on Rails.<\/p>\n
Conclusion<\/h2>\n
In this tutorial, we covered the process of building a social media app using Ruby on Rails. We walked through setting up a Rails application, creating models and associations, implementing user registration and authentication using Devise, and building the core functionality of a social media app including posting and commenting.<\/p>\n
From here, you can extend the functionality of your social media app by adding features such as likes, followers, and user profiles.<\/p>\n
Happy coding!<\/p>\n","protected":false},"excerpt":{"rendered":"
Introduction In this tutorial, we will walk through the process of building a social media app using Ruby on Rails. We will cover the basics of setting up a Rails application, creating models and associations, implementing user registration and authentication, and building the core functionality of a social media app, Continue Reading<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","footnotes":""},"categories":[1],"tags":[35,746,36,49,217],"yoast_head":"\nHow to Build a Social Media App with Ruby on Rails - Pantherax Blogs<\/title>\n\n\n\n\n\n\n\n\n\n\n\n\n\n\t\n\t\n\t\n