Home>

I am trying to implement the "Like" function while referring to this URL.
https://qiita.com/nojinoji/items/2c66499848d882c31ffa

However, I am getting the following error in a view that is trying to implement the "Like" feature.

I tried changing "message_likes_path" to "message_like_path" or adding ", method :: post" etc. in the corresponding error (line 22), but the error did not change.
The view file is as follows.

        % h3
            Likes: # {@ message.likes.count}
          --if current_user.already_liked? (@message)
            = button_to'Unlike', message_like_path (@message), method:: delete
          --other
            = button_to'Like', message_likes_path (@message)
          % h2 Liked user
          [email protected] message.liked_users.each do | user |
            % li = user.email
          = link_to "Return to Home", messages_path

The controller is below.
I wondered if the usage of instance variables was different, but the error did not change.

class MessagesController</pre>
<p>I would appreciate any advice.</p>
<hr />
<p><strong>[Updated 9/26]</strong><br />The above error statement said "undefined method` message_likes_path'", so I checked rails routes to find out the path.</p>
<pre><code>                Prefix Verb URI Pattern Controller # Action
        new_user_session GET/users/sign_in (.: format) devise/sessions # new
            user_session POST/users/sign_in (.: format) devise/sessions # create
    destroy_user_session DELETE/users/sign_out (.: format) devise/sessions # destroy
       new_user_password GET/users/password/new (.: format) devise/passwords # new
      edit_user_password GET/users/password/edit (.: format) devise/passwords # edit
           user_password PATCH/users/password (.: format) devise/passwords # update
                         PUT/users/password (.: format) devise/passwords # update
                         POST/users/password (.: format) devise/passwords # create
cancel_user_registration GET/users/cancel (.: Format) devise/registrations # cancel
   new_user_registration GET/users/sign_up (.: format) devise/registrations # new
  edit_user_registration GET/users/edit (.: format) devise/registrations # edit
       user_registration PATCH/users (.: format) devise/registrations # update
                         PUT/users (.: format) devise/registrations # update
                         DELETE/users (.: format) devise/registrations # destroy
                         POST/users (.: format) devise/registrations # create
                    root GET/messages # index
      user_message_likes POST/users /: user_id/messages /: message_id/likes (.: format) likes # create
       user_message_like DELETE/users /: user_id/messages /: message_id/likes /: id (.: format) likes # destroy
           user_messages GET/users /: user_id/messages (.: format) messages # index
                         POST/users /: user_id/messages (.: format) messages # create
               edit_user GET/users /: id/edit (.: format) users # edit
                    user PATCH/users /: id (.: format) users # update
                         PUT/users /: id (.: format) users # update

Since the prefix says "user_message_likes", I changed "message_likes_path" to "user_message_likes_path". In addition, I changed the argument to "m" in the top array and made the view file as follows.

.messages
  [email protected] messages.each do | m |
    .message
      .takerface
        % img {alt: "userface", src: "https://imgc.eximg.jp/i=https://imgc.eximg.jp/i=https%253A%252F%252Fs.eximg.jp%252Fexnews%252Ffeed%252FRen_ai%252FRen_ai_188420_6aed_1.jpg,zoom=600, quality = 70, type = jpg "} /
      .contents
        .takername
          = m.user.name + "to"
        .letter
          --if m.content.present?
            % p.lower-message__content
              = m.content
        .credo
          # Work lover # Intimacy # Life change
        .like% h3
            Likes: # {m.likes.count}
          --if current_user.already_liked? (M)
            = button_to'Unlike', user_message_like_path (m), method:: delete
          --other
            = button_to'Like', user_message_likes_path (m)
          % h2 Liked user
          --m.liked_users.each do | user |
            % li = user.email
          = link_to "Return to Home", messages_path

The error has changed a little and now looks like this:


[Updated 9/27]

The model code is below. From the top, "Message", "User", and "Like".

class Message
class User
class Like
  • Answer # 1

    / users /: user_id/messages /: message_id/likes /: id (.: format)Since it is, you have to pass three ids, user_id, message_id, and like id.

    --if current_user.already_liked? (M)
      --like = retrieve the like that current_user has set to m
      = button_to'Cancel Like', user_message_like_path (current_user.i, m.id, like.id), method:: delete

  • Answer # 2

    Thank you! I passed below!

            --if current_user.already_like_this? (m)
                --like = current_user.likes.find_by (message_id: m.id)
                = button_to'Unlike', user_message_like_path (current_user.id, m.id, like.id), method:: delete
              --other
                = button_to'Like', user_message_likes_path (current_user.id, m.id), method:: post

Related articles