[RUBY] [Personal application work memo] Move the location of the follow button

I installed the follow button before, I don't remember where I installed it before The place is pretty confusing It was a place that I wouldn't normally use there, so I will move it to a place that is easier to understand and use.

The destination is here. スクリーンショット 2020-08-25 午後7.28.35.png Next to current_user.name for each post.

First, let's go find the follow button that we made somewhere before.

I found it! !!

ruby:app/views/users/show.html.haml


.users-show
  = render 'follow_form'
  = render 'stats'

I'm calling it with this partial template.

Where are you calling from here?

ruby:app/views/users/_follow_form.html.haml


- unless current_user?(@user) 
  - if current_user.following?(@user)
    = render 'unfollow'
  - else
    = render 'follow'

This is also called with a partial template.

Where are you calling from here?

ruby:app/views/users/_unfollow.html.haml


= form_for(current_user.following_relationships.build(following_id: @user.id)) do |f|
  = f.hidden_field :following_id
  = f.submit "Follow", class: "btn btn-large btn-primary follow-btn"

ruby:app/views/users/_unfollow.html.haml


= form_for(current_user.following_relationships.find_by(following_id: @user.id), html: { method: :delete}) do |f|
  = f.submit "Unfollow", class: "btn btn-large btn-primary follow-btn"

I'll definitely get an error, but let's just paste this into the destination.

= render 'follow_form'

When I pasted it, I got this error.

ActionView::MissingTemplate in Posts#index

I was told that there was a problem with posts # index.

Wait a minute.

The file you are trying to call app/views/users/_follow_form.html.haml

To call app/views/posts/_post.html.haml

If the directory is different, you have to write it like that.

= render 'users/follow_form'

When I put it in this, no error appears, but it is not displayed. .. .. why? ??


Another discovery before this reason is resolved. I finally found a follow button. It also works fine. If you press the follow button, you can follow properly, It functions as a follow button. All you have to do is move the location of this.

posts_controller.rb


 def index
    @posts = Post.all.includes(:user).order("created_at DESC").page(params[:page]).per(5)
    @post = Post.new
    @user = current_user
 end

It looks like this was bad yesterday. The bad thing about this is that all @users become current_user. Now, no matter which user's link is clicked, it goes to the current_user's My Page and does not go to that person's My Page.

@user =The person who posted the post

I have to write.

After all, I decided not to put the follow button on the list screen

When I looked at Twitter, there was no follow button on the list screen. The follow button wasn't on the list screen, only after I went to the user's details screen. So don't put a follow button on the list screen.

Recommended Posts

[Personal application work memo] Move the location of the follow button
[Personal application work memo] Make a calendar with simple_calendar
[Personal memo] About button (GUI) operation of Docker Desktop for Windows
A memo that containerizes the simple chat application of Node.js + socket.io
[Personal application development work memo] Create an image list page like Instagram
Memo: [Java] Check the contents of the directory
[Personal] JUnit5 memorandum memo (work in progress)
Personal application production 2 Introduction of WEB fonts