Rails 4 acts_as_follower

时间:2016-06-06 08:11:01

标签: ruby-on-rails ruby

我有两个型号 用户和组 我正在使用gem acts_as_follower https://github.com/tcocca/acts_as_follower

User.rb

class User < ActiveRecord::Base
  has_many :groups
  acts_as_follower
end

和Group.rb

class Group < ActiveRecord::Base
  belongs_to :user
  acts_as_followable
end

指向关注和取消关注的链接

<% if current_user %>
            <% if current_user.following?(@group) %>
                <h2> <%= link_to 'Unfollow', unfollow_group_path(@group), class: 'fa fa-pause' %></h2>
            <% else %>
                <h2> <%= link_to 'Follow', follow_group_path(@group), class: 'fa fa-play' %></h2>
            <% end %>
        <% else %>
            <%= link_to 'Login or Register to Follow', user_session_path, class: 'fa fa-unlock' %>
        <% end %>

指向管理组关注者的链接

     <h5>followers 
<%= p  @group.followers.count %></h5>
            <% @group.followers.each do |follower| %>
                <%= link_to user_path(follower) do %>
                    <%= image_tag(follower.image.url(:show), size: "40x40") %>
                    <% if current_user.id == @group.user.id %>
                    <%= link_to 'Reject', unfollow_group_path(@group , user_id:follower.id), class: '   fa fa-close' ,  style: 'color:red;' %>
                    <% end %>
                <% end %>
            <% end %>
            <% if current_user.id == @group.user.id %>
            <h5>expect</h5>
            <% @followers_block.each do |block| %>
                <%= link_to user_path(block.follower) do %>
                    <%= image_tag(block.follower.image.url(:show), size: "40x40") %>
                        <%= link_to 'Accept', follow_group_path(@group , user_id:block.follower.id), class: '   fa fa-close' ,  style: 'color:red;' %>
                    <% end %>
                <% end %>
        <% end %>

groups_controller.rb

 def follow
    @group = Group.find(params[:id])
    if params[:user_id] && @group.user_id == current_user.id
      @user = User.find(params[:user_id])
      @user_block = Follow.where(followable_id: @group, followable_type: "Group" , follower_id: @user ).first
      @user_block.update_attribute(:blocked, false)
      @user_block.save
      redirect_to :back
    else
    if current_user
      current_user.follow(@group)
      flash[:notice] = 'are now following'
    else
      flash[:error] = "You must login to follow "
    end
    redirect_to :back
      end
  end

  def unfollow
    @group = Group.find(params[:id])
    if params[:user_id] && @group.user_id == current_user.id
      @user = User.find(params[:user_id])
      @user.stop_following(@group)
      redirect_to :back
    else
    if current_user
      current_user.stop_following(@group)
      flash[:notice] = 'You are no longer following .'
    else
      flash[:error] = 'You  to unfollow #'
    end
    redirect_to :back
    end
  end

如何添加参数(:已屏蔽,为真)

if current_user
          current_user.follow(@group)
          flash[:notice] = 'are now following'

或者

 <h2> <%= link_to 'Follow', follow_group_path(@group), class: 'fa fa-play' %></h2>

1 个答案:

答案 0 :(得分:0)

解决

if current_user
      follow = current_user.follow(@group)
      follow.update blocked: true
      flash[:notice] = 'are now following'