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.
↓ Thank you for the following article. Thank you very much.
--How to make a follow function with Rails
https://qiita.com/mitsumitsu1128/items/e41e2ff37f143db81897
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.
↓ 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)
.
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 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