Home  >  Article  >  Database  >  How to develop a simple social networking feature using MySQL and Ruby on Rails

How to develop a simple social networking feature using MySQL and Ruby on Rails

WBOY
WBOYOriginal
2023-09-21 08:45:421360browse

如何使用MySQL和Ruby on Rails开发一个简单的社交网络功能

How to develop a simple social network function using MySQL and Ruby on Rails

In today's digital age, social networks have become a part of people's lives. In order to meet the needs of users, it is necessary to develop a simple but fully functional social network application. This article will introduce how to develop a simple social network application using MySQL and Ruby on Rails, and provide specific code examples.

  1. Environment preparation
    Before you begin, make sure you have MySQL and Ruby on Rails installed. If you haven't installed it yet, you can find the installation guide on the official website.
  2. Create Rails Application
    Enter the following command at the command line to create a new Rails application:

    rails new social_network
  3. Configure the database
    In the project Find the config/database.yml file in the root directory, open and edit it. Populate the following into the appropriate positions:

    development:
      adapter: mysql2
      encoding: utf8
      database: social_network_development
      pool: 5
      username: your_mysql_username
      password: your_mysql_password
      host: localhost

    Replace your_mysql_username and your_mysql_password with your MySQL username and password.

  4. Create database
    Enter the following command at the command line to create the database:

    rails db:create
  5. Create user model
    Enter the following command To generate a model named User:

    rails generate scaffold User username:string email:string password:string

Then run database migration:

rails db:migrate

This will create a table named users in the database, containing username, email and password fields.

  1. Create a social network model
    Enter the following command to generate a model named Friendship:

    rails generate model Friendship user_id:integer friend_id:integer

    Then run the database migration command:

    rails db:migrate

    This will create a table named friendships in the database, containing user_id and friend_id fields, which are used to establish relationships between users.

  2. Set up the association
    Add the following code in the User model to establish the association with the Friendship model:

    class User < ApplicationRecord
      has_many :friendships
      has_many :friends, through: :friendships
    end

    This will allow a user to have multiple friendships, And you can get all its friends through the friends method.

  3. Writing Controller
    Create a file named friendships_controller.rb in the app/controllers directory and add the following code:

    class FriendshipsController < ApplicationController
      def create
     @friendship = current_user.friendships.build(friend_id: params[:friend_id])
     if @friendship.save
       flash[:success] = "Friend added"
     else
       flash[:error] = "Unable to add friend"
     end
     redirect_to root_url
      end
    
      def destroy
     @friendship = current_user.friendships.find_by(friend_id: params[:id])
     @friendship.destroy
     flash[:success] = "Friend removed"
     redirect_to root_url
      end
    end

    These codes define Methods to create and delete Friendship objects.

  4. Update Views
    Open the app/views/users/show.html.erb file and add the following code:

    <% @user.friends.each do |friend| %>
      <p><%= friend.username %>: <%= link_to "Remove Friend", friendship_path(friend), method: :delete, data: { confirm: "Are you sure?" } %></p>
    <% end %>
    
    <h1>Add Friend</h1>
    <%= form_tag friendships_path, method: :post do %>
      <%= hidden_field_tag :friend_id, @user.id %>
      <%= submit_tag "Add Friend" %>
    <% end %>

    This will be on the user's personal page His friends list is displayed and friends can be added or removed with the click of a button.

  5. Run the application
    Enter the following command in the command line to start the application:

    rails server

Now you can visit http://localhost :3000 to use your simple social networking application!

Summary:
Through the introduction of this article, you have learned how to use MySQL and Ruby on Rails to develop a simple social network function. By creating user models and friendship models, and establishing corresponding associations and controllers, you can implement relationships between users and the functions of adding and deleting friends. I hope this article is helpful to you, and I wish you smooth development!

The above is the detailed content of How to develop a simple social networking feature using MySQL and Ruby on Rails. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn