综合编程

Infinite demand on ajax

微信扫一扫,分享到朋友圈

Infinite demand on ajax
0 0

I am working on an app where user can heart(like) stories. I am trying to implement it with ajax. I have a StoriesController with the heart action. Whenever the heart(like) is clicked, I have responded with heart.js.erb and send a post request. I am working on to update the number of hearts when the user clicks the heart link. But what I am getting is infinite requests via ajax. Below is the snippet of heart action.

# Give your heart to someone
  def heart
    respond_to do |format|
      format.js
      format.html
    end
  end

And ajax request is:

$('#heart-story-<%= j params[:id]%>').html('<%= @hearts %>')
$.post("/stories/<%= j params[:id]%>/heart", <%= j params[:id] %>)
console.log("<%= j params[:id] %>")

What is the probable reason that I am getting infinite request? The route is:

#Stories
resources :stories, only: [:show, :create, :destroy] do
member do
  get :heart, :unheart
  post :heart, :unheart
end
# Comments
resources :comments, only: [:index, :new, :create, :destroy]
end

If you have specified following code in heart.js.erb then it will be infinite:

$('#heart-story-<%= j params[:id]%>').html('<%= @hearts %>')
$.post("/stories/<%= j params[:id]%>/heart", <%= j params[:id] %>)
console.log("<%= j params[:id] %>")

Post request should only be triggered on button click for once don't use it in heart.js.erb

Hello, buddy!
感谢您的支持!

    Divisions do not float properly

    上一篇

    New Encryption Mode Brings Sincerity and Discretion to Low-Cost Android Devices

    下一篇

    您也可能喜欢

    评论已经被关闭。

    插入图片

    热门分类