I am creating a blog posting app that uses ActionText to create a portfolio.
I want to add a follow functionHow to make a follow function in RailsWe are proceeding with the implementation with reference to the article.

I want to be able to follow on the details page of my blog, but I don't see the button to follow.
I tried to solve the error by referring to the question of the person who referred to the same article in the past in Terratail, but it did not work.

Corresponding source code

relationships table

class CreateRelationships</pre>
<p><br />
Relationship model association</p>
<pre><code>class Relationship</pre>
<p>user model association</p>
<pre><code>class User</pre>
<p><br />relationships controller</p>
<pre><code>class RelationshipsController</pre>
<p><br />
posts controller</p>
<pre><code>class PostsController</pre>
<p><br />
Follow button<br />
<pre><code><% if user_signed_in?%><% unless current_user == user%><% if current_user.following? (User)%>  <% = form_for (current_user.relationships.find_by (follow_id: user.id), html: {method :: delete}) do | f |%>    <% = hidden_field_tag: follow_id, user.id%>    <% = f.submit'Unfollow', class:'btn btn-danger btn-block'%>  <% end%><% else%>  <% = form_for (current_user.relationships.build) do | f |%>    <% = hidden_field_tag: follow_id, user.id%>    <% = f.submit'Follow', class:'btn btn-primary btn-block'%>  <% end%><% end%><% end%><% end%>


Rails.application.routes.draw do
  devise_for: users, only: [: show]
  devise_for: users, controllers: {
root to: "posts # index"
  resources: posts do
    resources: comments, only: [: create,: destroy]
    collection do
  resources: users, only: [: show,: index,: search]
  resources: relationships, only: [: create,: destroy]


        <% = image_tag @ post.image.variant (resize: '800x800'). processed%>  <br>  <br>  <br>        <strong><% = @ post.title%></strong>              <% if @ post.user.image.attached?%>  <% = image_tag @ post.user.image, class: "avatar m-4"%>  <% else%>  <% = image_tag ("defo.jpg"), class: "avatar m-4"%>  <% end%>      " ,,>    <p><% = @ post.user.nickname%></p>        <% = render partial:'posts/posts', collection: @posts, as :: post%>    <% = render'relationships/follow_button', user: @user%>      <% = @ post.content%>  <hr>   Comments<% = @comments_count%><% if @comments%><% @ comment.each do | comment |%>  <% if user_signed_in?%>    <p>    <strong><% = link_to comment.user.nickname, "/ users/# {comment.user_id}"%>:</strong>    <% = comment.text%>    </p><% else%>  <p>    <strong><% = comment.user.nickname%>:</strong>    <% = comment.text%>  </p><% end%><% end%>                    <% if current_user%>    <hr>        <% = form_with (model: [@post, @comment],

 local: true) do | form |%>                          <% = form.text_area: text, placeholder: "comment", rows: "2", class: "form-control"%>                              <% = form.submit "SEND", class: "btn btn-outline-secondary"%><% end%>                                      <% else%>        <strong><p>※※※ Please log in to comment ※※※</p></strong>      <% end%>                <% end%>  
What I tried

As was the comment section of the original article and this questionrelationships controllerDescription of
I corrected the part of params [: relationship] [: follow_id] to params [: follow_id].
Then you will get the following error.

undefined method ʻid'for nil: NilClass

After that, I modified the hidden_field_tag: follow_id, user.id part to f.hidden_field: follow_id, user.id, but the error content did not change.

When I defined @user in the show method of posts_controller.rb which is displaying the view where the error message has no id, the error disappeared but the follow button was not displayed.

I thought about it myself and tried various things, but I couldn't solve it.

Thanks for your consideration.

Supplementary information (FW/tool version, etc.)

ruby_version 2.6.5


  • Answer # 1

    First and last
    <% unless current_user == user%>
    <% end%>
    Is it displayed when you delete?
    If so, there are hints for a solution.

    That means that you haven't registered and tried multiple users.
    <% unless current_user == user%>
    This means that if the person who is currently open is not the user, the button will be displayed. "
    So please register two or more users and post each one.

    This is because there is before_action: set_user, but that set_user is not defined.
    The methos name is dropped in the def directly under private

  • Answer # 2

    Of the controllersposts # showIf you extract only the part related to

    class PostsController</pre>
    <p>Don't you notice something strange?<br />
    Easy to understand<code>params [: id]</code>Extract only about</p>
    <pre><code data-language = "Ruby">  user = User.find (params [: id])
        @user = User.find (params [: id])
          @post = Post.find (params [: id])

    params [: id]It's a good idea to rethink what is.

    It ’s not related to the question,posts # showI have the impression that displaying all posts at will be a heavy load as the scale increases.

Related articles