I want to add a like function to the post list page.

Asked 2 months ago, Updated 2 months ago, 2 views

Prerequisites/What you want to achieve

I want to add a like function to the post list page

Problems/Error Messages you are experiencing

We are currently implementing a like function on the post details page (show).
Enter a description of the image here

If you enter the source code below on the post list page (index), you will receive this error.
post_images/index.html.erb

<section class="sct-color-1slice">
  <div class="container-fluid reason-container">
    <div class="row">
      <div class="col-md-8col-sm-offset-2">
        <div class="masonry-wrapper-cols">
          <div class="masonry-gutter"></div>
            <%@post_images.each do | post_image | %>
            <div class="masonry-block">
              <div class="block block-image v1">
                <div class="block-image">
                  <div class="view view-first">
                  <%=link_to post_image_path(post_image.id)do%>
                    <%=attachment_image_tag post_image, :image%>
                  <%end%>
                  </div>
                </div>
                <div class="block-content">
                  <%=attachment_image_tag post_image.user, :profile_image, size: "100x100", fallback: "no_image.jpg", class: "img-circle pull-left profile-thumb"%>
                  <h3class="block-title">
                      <%=post_image.image_name%>
                  </h3>
                  <ul class="inline-meta">
                    <li>By
                      <%=post_image.user.name%>
                    </li>
                    <li>
                      <%=link_to"#{post_image.post_comments.count}Comment", post_image_path(post_image.id)%>


                    </li>
                    <%[email protected]_image.favored_by?(current_user)%>
                      <li>
                        <%=link_to post_image_favorites_path(@post_image), method::delete do%>
                          <ic class="fa fa-star" aria-hidden="true" style="color:range;font-size:15px;">/i>
                          <%[email protected]_image.favorites.count%>fighting
                        <%end%>
                      </li>
                    <%else%>
                      <li>
                        <%=link_to post_image_favorites_path(@post_image), method::post do%>
                          <i class="fa fa-star" aria-hidden="true" style="font-size:15px;">/i>
                          <%[email protected]_image.favorites.count%>fighting
                        <%end%>
                      </li>


                    <%end%>
                  </ul>
                </div>
              </div>
            </div>
            <%end%>
            <%[email protected]_images, class:"paginate"%>
        </div>
      </div>
    </div>
  </div>
</section>

Error Contents
Enter a description of the image here

Source Code

post_image.rb(model)

class PostImage<ApplicationRecord
    belongs_to —user
    attachment:image
    has_many:post_comments,dependent::destroy
    has_many:favorites, dependent::destroy
    values:image_name,presence:true
    values:image,presence:true
    deffavored_by?(user)
        favorite.where(user_id:user.id).exists?
    end
end

post_images_controller.rb(controller)

class PostImagesController<ApplicationController
    def new
        @post_image=PostImage.new
    end

    def create
        @post_image=PostImage.new(post_image_params)
        @post_image.user_id=current_user.id
        [email protected]_image.save
          redirect_to post_images_path
        else
          render:new
        end
    end

    def index
        @post_images=PostImage.page(params[:page]).reverse_order
    end

    def show
        @post_image=PostImage.find (params[:id])
        @post_comment=PostComment.new
    end

    def destroy
        @post_image=PostImage.find (params[:id])
        @post_image.destroy
        redirect_to post_images_path
    end

    private

    def post_image_params
        param.require(:post_image).permit(:image_name,:image,:caption)
    end
end

Supplementary Information

I understand that it is not defined, but I don't know how to fix it.
I would appreciate it if you could let me know.

Rails 5.2.4.1
ruby 2.5.7p206 (2019-10-01 revision 67816) [x86_64-linux-gnu]

Add

If @post_images is set to post_image, the likes button will now be displayed in the post list.

As a new error, if you press like, it will fly to the detail page.
I think it's because it's link_to post_image_favorites_path(@post_image), but even if I write post_images_path, the root of index, I get a routes error.

favorite_controller.rb(controller)

class FavoritesController<ApplicationController
    def create
        post_image=PostImage.find (params[:post_image_id])
        favorite=current_user.favorites.new(post_image_id:post_image.id)
        favorite.save
        redirect_to post_image_path(post_image)
    end

    def destroy
        post_image=PostImage.find (params[:post_image_id])
        favorite=current_user.favorites.find_by(post_image_id:post_image.id)
        favorite.destroy
        redirect_to post_image_path(post_image)
    end
end

ruby-on-rails

2022-09-30 11:02

1 Answers

After changing @post_images to post_image, I was able to display the likes function in the post list.

The reason is that index.html.erb stored @post_images in the variable post_image such as <%@post_images.each do | post_image|%>.


2022-09-30 11:02

If you have any answers or tips


© 2022 OneMinuteCode. All rights reserved.