I want to use javascript as asynchronous for the likes function of rails.

Asked 3 months ago, Updated 3 months ago, 6 views

I have implemented it by referring to the site (http://qiita.com/YuitoSato/items/94913d6a349a530b2ea2), but there are two things I don't understand.I look forward to hearing from you.

As for the current situation, I do not understand these two points and implement them without asynchronous processing as shown below.
It is working normally except for asynchronous.
いいThe like icon uses fa-thumbs-o-up of Font-Awesome.

↓ show.html.erb

<%if answer.like?(current_user)%>
            <%=link_to like_path(@likes, answer_id:answer.id), method::delete do%>
              <ic class="fa fa-thumbs-o-up">/i>/a>
              <span>
                <%=answer.like_count%>
              </span>
            <%end%>
          <%else%>
            <%=link_to likes_path(answer_id:answer.id), method::post do%>
              <ic class="fa fa-thumbs-o-up">/i>/a>
              <span>
                <%=answer.like_count%>
              </span>
            <%end%>
          <%end%>

↓ answer.rb

def like_count
    likes=Like.where (answer_id:self)
    likes.count
end
def like?(current_user)
    Like.where(user_id:current_user.id, answer_id:id)
end

↓questions_controller.rb

def show
    @answers=Answer.enable_answer.where (question_id:@q uestion.id)
    @category=Category.find_by (id:@question.category_id)
    @favorite=Favorite.find_by(user_id:current_user.id,question_id:@q uestion.id)
    @like=Like.where (answer_id:params[:answer_id])
end

↓ likes_controller.rb

class LikeController<AuthorizedController
  before_action —set_answer
  after_action —set_likes
​
  def create
    like=Like.new(user_id:current_user.id, answer_id:params[:answer_id])
    like.save
    redirect_to question_path(@answer.question_id)
  end
​
  def destroy
    like=Like.find_by (user_id:current_user.id, answer_id:params[:answer_id])
    like.destroy
    redirect_to question_path(@answer.question_id)
  end
​
  private
​
  default_answer
    @answer=Answer.find (params[:answer_id])
  end
​
  default_likes
    @like=Like.where (answer_id:params[:answer])
  end

↓ answer_controller.rb

def show
    @answer=Answer.find (params[:id])
    @like=Like.where (prototype_id:params[:id])
  end

javascript ruby-on-rails ruby

2022-09-30 13:49

1 Answers

The implementation of this like is not modern, so I recommend you to think about using React or Vue.js.

If you want to implement it casually, I will do this

The JavaScript neighborhood is a highly variable neighborhood, so we omit detailed implementation methods.The basic usage is detailed on the official website, so please check it out.

  • 1 railsg controller api/likes
  • Implement like function for create action of 2api/likes controller
  • Implementation of 3api/likes controller destroy action to remove likes
  • Implementing Count Display
  • Like button implementation
  • 1 Implement the ability to throw requests to api/likes controller when a like button is pressed
  • Implementation of count up and count down when 21 is pressed

I think it would be best to implement it in API mode implemented from Rails 5 and static HTML/CSS/JS, but I wrote down how to implement it only with Rails.


2022-09-30 13:49

If you have any answers or tips


© 2023 OneMinuteCode. All rights reserved.