A like function using asynchronous communication cannot be implemented.

Asked 2 years ago, Updated 2 years ago, 440 views

What I want to do is implement a like function using asynchronous communication (on the video posting page)
As an overview of the portfolio, we separate pages dedicated to posting videos from pages dedicated to tweeting.
I can now go to the Post List page, but the asynchronous communication is no longer working.

This is the URL of the video.
https://gyazo.com/33390355c75c2a7d6a17ed0878109781

Code for writing asynchronous processing
create.js.erb

 document.getElementById('like_<%[email protected]%>').innerHTML='<%=j(render@movie)%>'

destroy.js.erb

 document.getElementById('like_<%[email protected]%>').innerHTML='<%=j(render@movie)%>'

View Files

<div class="content-wrapper">
    <div class="content-block">
        <%@movies.each do | movie | %>
        <div class="content">
         <div class="user-about">
             <div class="image">
             <%if movie.user.image.attached?%>
             <%=image_tag movie.user.image%>
                <%else%>
            <%=image_tag no.user.png%>
                <%end%>
             </div>

         <div class="profile">
            <div class="name-history">
                <div class="name">

                <%=movie.user.nickname%>


                </div>
                <div class="mania-history">
                    <%="Learning history: #{movie.user.mania_history} years"%>
                </div>
             </div>
             
             <div class="enjoy-point">
             <%="Fun Points#{movie.user.enjoy_point}"%>
             </div>
         </div>
       </div>
       
       <div class="text">
       <p>Title: <%=movie.title%></p>br>
       <p>Description: <%=movie.introduction%>/p>
       <div class='video'>
       <video src=<%=rails_blob_path(movie.movie)%>type="movie/mp4", controls></video>
       </div>
    </div>
       
         <div class="action-menu">

            <trid="post_<%=movie.id%>">
            <%=render 'like', movie:movie%> 
            </tr>
             <div class="comment">
                <%if user_signed_in?%>
                <h3>Number of comments: <%=movie.comments.count%>/h3>
                <%=link_to "Comment", "/movies/#{movie.id}", class: "comment-button"%>
                <%else%>
                <h3>Number of comments: <%=movie.comments.count%>/h3>
                <%=link_to "View Comments", "/movies/#{movie.id}", class: "comment-button"%>
                <%end%>
            </div>
                
            <div class="user-action">
            <%if user_signed_in?%>
            <%if current_user.id==movie.user.id||user_signed_in%>
            <%=link_to "edit", edit_movie_path(movie)%>
            <%=link_to "delete", movie_path(movie), method::delete%>
            <%end%>
            <%end%>
            </div>
         </div>

        </div>
        <%end%>

    </div>
    <div class="sidebar">
     <div class="box">

     </div>
     <div class="box">
         
    </div>
    </div>
</div>

View File Partial Template

<%if user_signed_in?%>
             <div class="like">
             <h3>Number of likes: <%=post.likes.count%>/h3>
             <div class='like-button'>
             <%if current_user.like_by?(post.id)%>
                <td><%=link_to 'unlike', destroy_like_path(post), class: "like-link", method::DELETE, remote:true%></td>
                <i class="fa fa-heart unlike-btn">/i>
            <%else%>
                <td><%=link_to 'like', create_like_path(post), class: "like-link", method::create, remote:true%></td>
                <i class="fa fa-heart like-btn">/i>

             <%end%>

            </div>
            </div>
            <%end%>

User related models

class User<ApplicationRecord
  # Include default device modules.Others available are:
  # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
  device:database_authenticatable, :registerable,
         :recoverable, :rememberable, :validatable
    
  has_one_attached —image
  has_many —posts
  has_many —like
  has_many:comments,dependent::destroy
  has_many —movie_likes
  has_many —movies
  
 
  defliked_by?(post_id)
    likes.where(post_id:post_id).exists?
  end

  def movie_like_by?(movie_id)
    movie_like.where(movie_id:movie_id).exists?
  end

  with_options presence —true do
    validates —nickname
    validates —mania_history
    values:enjoy_point
    validates:email
    values:password,length: {minimum:6}

  end

  validate:image_presence

  image_presence
    if image.attached?
      if!image.content_type.in?(%('image/jpeg image/png')
        US>errors.add(:image, ' should be accompanied by jpeg or png file')
      end
    end
  end
end

Video Posting Models

class Movie <ApplicationRecord
  has_one_attached —movie
  has_many:like_users,through::like,source::user
  has_many:comments,dependent::destroy
  belongs_to —user
  has_many —movie_likes

  with_options presence —true do
        validates:title
        validates:introduction
        validates:movie

    end

    validate:movie_presence

    def movie_presence
      if movie.attached?
        if movie.content_type.in?(%('movie/mp4 movie/mov')
          errors.add(:movie, ' must be accompanied by mp4 or move file)')
        end
      else
        errors.add(:movie, 'Please attach file')
      end
    end 
end

Like feature models

Supplemental:
"When implementing the ""Like"" function, we separate ""Like.rb"" from ""Like.rb"" (movie_like.rb) in the ""Like"" function, which is the same for the controller."

"

movie_like.rb

class MovieLike<ApplicationRecord
    belongs_to —user
    belongs_to —movie
end
class MovieLikeController<ApplicationController
  before_action —movie_params

  def create
    MovieLike.create(user_id:current_user.id, movie_id:params[:id])
    redirect_to movies_path
  end

  def destroy
    MovieLike.find_by(user_id:current_user.id, movie_id:params[:id]).destroy
    redirect_to movies_path
  end

  def movie_params
    @movie=Movie.find (params[:id])
  end
end

movies_controller

class MoviesController<ApplicationController
        before_action:authenticate_user!, only:[:new,:update,:create,:edit,:update,:destroy]
        before_action: find_movie, only: [:edit,:update,:show,:destroy]
      
          def index
              @movies=Movie.all.order(id: "DESC")
              @movie_like=MovieLike.new
      
            end
      
            def new
              @movie=Movie.new
      
            end
      
          def show
              @ comment = Comment.new     
              @[email protected](id: "DESC")
          end
          
          def create
            @movie=current_user
            @movie=Movie.create(movie_params)
            [email protected]
              redirect_to movies_path, notice: 'Post Successful'
              else
                redirect_to new_movie_path, notice: 'Post failed'
              end
          end
          
            default  
            end
      
            default update
              @movie.update(movie_params)
              redirect_to movies_path
            end
      
            def destroy
              [email protected] store
                redirect_to movies_path, alert: 'Post deleted'
              else
                redirect_to movies_path
              end
            end
      
            private
      
              def movie_params
                param.require(:movie).permit(:title,:introduction,:movie).merge(user_id:current_user.id)
              end
      
              def find_movie
                @movie=Movie.find (params[:id])
              end
      
              def force_redirect_unless_my_movie
                return redirect_to movies_path, alert: 'You do not have permissions' [email protected]!=current_user
              end
      
end

That's it

javascript ruby-on-rails mysql

2022-09-30 21:56

1 Answers

<trid="post_<%=post.id%>">
  <%=render 'post', post:post%>
</tr>

I changed tr to div and implemented it.


2022-09-30 21:56

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.