[Rails] (Supplement) Implemented follow function

Overview

After following the reference article below, I was able to implement the follow function.

Most of the implementation methods are described in the reference article, so please have a look there.

In this article, we will record the supplementary content as a memorandum.

Reference article

↓ Thank you for the following article. Thank you very much.

--How to make a follow function with Rails
https://qiita.com/mitsumitsu1128/items/e41e2ff37f143db81897

Complete image

This time, we will explain how to create a follow function and set a follow button on the user page and photo detail page using the photo posting app as the theme.

--User page Image from Gyazo

--Photo detail page Image from Gyazo

Development environment

Implementation flow

  1. Create a follow function
  2. Install follow button on user page and photo detail page

This code

1. Create a follow function

↓ Please refer to the following m (_ _) m

--How to make a follow function with Rails
https://qiita.com/mitsumitsu1128/items/e41e2ff37f143db81897

As a side note, I modified the code in relationships_controller.rb as follows, as commented by the reference article.

relationships_controller.rb


  private
  def set_user
    @user = User.find(params[:follow_id])
  end

--Since the method name was omitted in the reference article, I added set_user. --Changed @ user = User.find (params [: relationship] [: follow_id]) to @ user = User.find (params [: follow_id]). When I checked the contents of params in binding.pry, the key : relationship did not exist and an error occurred. Therefore, I deleted [: relationship].

↓ It is the contents of params confirmed on the console

[1] pry(#<RelationshipsController>)> params
=> <ActionController::Parameters {(abridgement)"follow_id"=>"1", "commit"=>"To follow", "controller"=>"relationships", "action"=>"create"} permitted: false>

relationships_controller.rb


    if following.save
      flash[:success] = 'Followed the user'
      redirect_to user_path(@user)

--Fixed the transition destination path after clicking the follow button. This time, for the time being, after clicking, I made it transition to the user page with redirect_to user_path (@user).

2. Install follow button on user page and photo detail page

According to the reference article, I created a follow button in a partial template called _follow_button.html.erb.

By calling this partial template on the user page and the photo detail page, follow buttons will be installed on both pages.

For example Call the follow button partial template by typing <% = render'shared/follow_button', user: @user%>.

In @ user, the record of the user to follow is stored in each controller.

For example, on the photo detail page, I got the photo record from the photo id in params, and also got the user record associated with it and stored it in @ user.

↓ like this

photos_controller.rb


  def show
    @user = Photo.find(params[:id]).user
  end

I'm using find because the behavior is strange with faind_by.

in conclusion

In this way, we implemented the follow function. The code in the referenced article is difficult and I haven't fully understood it yet. I hope you will understand it one by one.

Please refer to the following articles for details on the association of each table in the follow function. https://bit.ly/2WztOY9

** Because it is a beginner, please point out any mistakes or improvements m (_ _) m **

Recommended Posts

[Rails] (Supplement) Implemented follow function
Rails follow function
[Rails] Implemented hashtag function
Implemented follow function in Rails (Ajax communication)
[Rails 6] Asynchronous (Ajax) follow function is implemented
Implement follow function in Rails
Implemented mail sending function with rails
[Rails 6] Ranking function
[Ruby on Rails] Follow function implementation: Bidirectional
Implemented comment function
[Rails] Notification function
Posting function implemented by asynchronous communication in Rails
How to make a follow function in Rails
[Rails, JavaScript] Implemented like function (synchronous / asynchronous communication)
Follow function (Ajax) implementation
[Rails] Implement search function
[rails] tag ranking function
Rails search function implementation
Implement user follow function in Rails (I use Ajax) ②
[For Rails beginners] Implemented multiple search function without Gem
Rails application guest login function implemented (devise not used)
Implement application function in Rails
Rails fuzzy search function implementation
[Rails] Implement User search function
Introduced graph function with rails
Search function using [rails] ransack
[Rails 6] Implementation of search function
Finally implemented Rails Form object
[Rails] Implementation of category function
Rails ~ Understanding the message function ~
Login function implementation with rails
[Rails] EC site cart function
Ajax bookmark function using Rails
[Rails] Implementation of tutorial function
[Rails] Implement image posting function
[Rails] Implementation of like function
[Rails] Implemented a pull-down search function for Active Hash data
[Rails 6] Pagination function implementation (kaminari)
[Rails] Implementation of CSV import function
[Rails] Asynchronous implementation of like function
Implemented authentication function with Spring Security ②
Implemented authentication function with Spring Security ③
[Ruby on Rails] Introduced paging function
[Rails] Implementation of image preview function
[Rails] Tag management function (using acts-as-taggable-on)
Kaminari --Added pagination function of Rails
[Rails] About implementation of like function
[Rails] Implementation of user withdrawal function
[Rails] Implementation of CSV export function
Create pagination function with Rails Kaminari
Implement simple login function in Rails
Implemented authentication function with Spring Security ①
[Ruby on Rails] CSV output function
[Rails] Voice posting function ~ Cloudinary, CarrierWave
[Rails] gem ancestry category function implementation
[Ruby on Rails] Comment function implementation
[Ruby on Rails] DM, chat function
[Rails 6] Like function (synchronous → asynchronous) implementation
Implement CSV download function in Rails
[Rails] Comment function implementation procedure memo
[Rails] I will explain the implementation procedure of the follow function using form_with.