Rails: Ajax for Like button when iterating through status up
Rails: Ajax for Like button when iterating through status updates
在我的用户索引操作中,我迭代所有发布状态更新的用户,并通过created_at DESC对其进行排序。
在我的index.html.view 中
<% @users.each.do |user| %>
<% user.status = @status %>
<%= @status.content %>
<div id ="like_form">
<% if @status.has_like_already?(@status, current_user)%>
<%= render 'already_liked'%>
<%else%>
<%= render 'like'%>
<%end%>
</div>
_like.html.erb
<%= link_to "Like", likes_path(:like_for => @status.id), remote: true, :method=>:post %>
_already_liked.html.erb
<span>Liked</span>
create.js.erb
$("#like_form").html("<%= escape_javascript(render('users/already_liked')) %>")
问题是,我可以喜欢状态更新,第一次发布时,"喜欢"按钮变为"不喜欢",但在随后的点击中,"赞"按钮不会变为"不同"。这是因为第一篇文章的#like_form
已经更改为"不同"。既然我在迭代,我该如何为发布的每个状态获得类似的功能来使用Ajax?
您可以将用户id添加到div,这样您就可以唯一化div的id。
index.html.erb
<div id ="like_form_<%= user.id %>">
</div>
_like.html.erb
<%= link_to "Like", likes_path(:like_for => @status.id, :user_id=>user.id), remote: true, :method=>:post %>
create.js.erb
$("#like_form_<%= params[:user_id]%>").html("<%= escape_javascript(render('users/unlike')) %>")
相关文章:
- jQuery mobile not going through ID
- Jquery iterating table
- 如何将bind()pass-through(to on())转换为delegate()-就像jQuery 1.7.1代码
- Traversing through a html table
- jQuery .each not iterating through li
- loop through json javascript
- JavaScript && JQuery : Iterating
- Loop through json with timeout variabe, javascript
- JavaScript React Authentication through Ruby Gem Devise?
- FileSize through javascript
- Looping through JQuery Id's
- Giving an object multiple properties through 'new'?
- Rails: Ajax for Like button when iterating through status up
- Looping Through JSON
- AngularJS的ng-repeat through json数组在第二个数组中不起作用
- Iterate through JSON from MapQuest API
- Loop through setTimeout
- Writing 1 through 125 in javascript
- Loop through JSON with javascript
- Set Value of input type in <td> through javascript