Я использую act_as_follower gem. Используя примерный код, он отлично работает для пользователя, следующего за другим объектом пользователя. Но я также хочу, чтобы Пользователь выполнял статью.act_as_follower для нескольких моделей
Код выглядит довольно простым, за исключением того факта, что FollowsController, по-видимому, закодирован специально для объекта пользователя. Должен ли я иметь отдельные действия для создания на каждом типе объекта?
Контроллеры/follows_controller.rb
class FollowsController < ApplicationController
def create
@user = User.find(params[:user_id])
current_user.follow(@user)
end
def destroy
@user = User.find(params[:user_id])
current_user.stop_following(@user)
end
end
модели/user.rb
...
acts_as_followable
acts_as_follower
...
модели/article.rb
class Article < ActiveRecord::Base
...
acts_as_followable
...
end
просмотров/следует/create.js.erb
$('#follow_user').html('<%= escape_javascript(render :partial => "shared/follow_user", :locals => {:user => @user}) %>');
просмотров/пользователей/show.html.erb
...
<% if user_signed_in? %>
<div id="follow_user">
<%= render :partial => "shared/follow_user", :locals => {:user => @user} %>
</div>
<% end %>
просмотров/статьи/index.html.erb
...
<% if current_user.following?(article) %>
<%= button_to("Un-Follow #{article.id}", article_follow_path(article.to_param, current_user.get_follow(article).id), :method => :delete, :remote => true) %>
<% else %>
<%= button_to("Follow #{article.id}", article_follows_path(article.to_param), :remote => true) %>
<% end %>
config/routes.rb
resources :articles do
resources :follows, :only => [:create, :destroy]
end
resources :users do
resources :follows, :only => [:create, :destroy]
end
Отличный ответ. Огромное спасибо. – ardochhigh