如何检查current_user是否使用Acts-as-Follower阻止了目标用户

时间:2018-06-21 18:32:55

标签: ruby-on-rails ajax activerecord acts-as-follower

我无法检查current_user是否阻止了目标用户。我正在尝试为阻止/取消阻止功能创建一个切换按钮。我不知道人们如何做到这一点,除非他们的解决方案还没有准备好AJAX。如何检查current_user是否跟随目标@user对象?

_block_user.html.erb

<% # unless @user == current_user %>
  <% **if current_user.blocking?(@user)** %>
    <%= link_to(unblock_user_path(@user), :remote => true, :class => 'btn btn-outline-danger') do %>
      <i class="fa fa-stop-circle"></i>
      Unblock
    <% end %>
  <% else %>
    <%= link_to(block_user_path(@user) ,:remote => true, :class => 'btn btn-outline-primary') do %>
      <i class="fa fa-play-circle"></i>
      Block
    <%end%>
  <% end %>
<% # end %>

2 个答案:

答案 0 :(得分:1)

如果它是AJAX的按钮,则必须具有类似的内容

<% if (!current_user.liked? @user) %>

        <%= link_to like_user_path(@user), class:"like-btn", method: :put, remote: true do %> 
        <i class="fa fa-stop-circle"></i>
        <% end %>  

    <% else %>

        <%= link_to like_user_path(@user), class:"like-btn", method: :put, remote: true do %> 
        <i class="fa fa-play-circle"></i>
        <% end %> 

 <% end %>

like.js.erb

<% if current_user.liked? @user %>
     document.getElementsByClassName('like-btn')[0].className = "like-btn liked";
<% else %>
     document.getElementsByClassName('like-btn')[0].className = "like-btn disliked";
<% end %>

控制器/用户

def like
    if !current_user.liked? @user
      @user.liked_by current_user
    elsif current_user.liked? user
      @user.unliked_by current_user 
    end
    respond_to do |format|
        format.html { redirect_back(fallback_location: root_path) }
        format.js 
        end
  end 

答案 1 :(得分:0)

我的方法来确定用户是否被阻止。 (未经current_user检查器测试)

<% #unless @user == current_user %>
<div id="block_user<%= @user.id %>">
  <% if current_user.blocks.include?(@user) %>
    <%= link_to(unblock_user_path(@user), :remote => true, :class => 'btn btn-outline-danger') do %>
      <i class="fa fa-stop-circle"></i>
      Unblock
    <% end %>
  <% else %>
    <%= link_to(block_user_path(@user), :remote => true, :class => 'btn btn-outline-primary') do %>
      <i class="fa fa-play-circle"></i>
      Block
    <% end %>
  <% end %>
</div>
<% # end %>