Rails 多态 activity 提要关系(多级)

Rails polymorphic activity feed relations (multiple level)

我正在处理多态 activity 提要,并尝试将来自不同模型的提要项目混合在一个页面中。

我有以下这些型号

供稿项目包括

模型关系

class User < ActiveRecord::Base
   has_one :dashboard
   has_many :activities
   has_many :works
   has_many :projects
   has_many :project_records
end

class Work < ActiveRecord::Base
   belongs_to :user
end

class Project < ActiveRecord::Base
   belongs_to :user
   has_many :project_records
end

class Dashboard < ActiveRecord::Base
   belongs_to :user
   has_many :activities
end

class Activity < ActiveRecord::Base
   belongs_to :user
   belongs_to :subject, polymorphic: true
end

我在用户模型中定义了订阅源,一个用户可以关注其他订阅源。

def feed
    following_ids = "SELECT followed_id FROM user_followings
                     WHERE  follower_id = :user_id"
    Activity.where("user_id IN (#{following_ids})
                     OR user_id = :user_id", user_id: id)
end

要构建多态 activity 提要,我在 WorkProject、[=49] 中有 "after_create" =]ProjectRecord模型.

after_create :create_activity

    private

      def create_activity
        Activity.create(
          subject: self,
          user: user
        )
      end
end

然后,我尝试列出提要(work, project, project record)在仪表板(控制器)/显示(操作)页面中。

这是我的仪表板控制器

class DashboardsController < ApplicationController

  before_action :authenticate_user!
  before_action :only_current_user    

  def show
    @feed_items = current_user.feed.order(created_at: :desc)
  end

  private
  def only_current_user
      @user = User.find( params[:user_id] )
      redirect_to(root_url) unless @user == current_user
  end    
end

在dashboard/show视图中

<% if @feed_items.any? %>

    <div class="feed-listing">
       <% @feed_items.each do |feed| %>
          <% if feed.subject_type == 'Work' %>
            <%= link_to polymorphic_path(feed.subject) do %>
              <%= render "activities/work_feed", subject: feed.subject, :feed => feed %>
              <%#= feed.subject.title %>
            <% end %>
          <% elsif feed.subject_type == 'Project' %>
            <%= link_to polymorphic_path(feed.subject) do %>
              <%= render "activities/project_feed", subject: feed.subject, :feed => feed %>
            <% end %>
          <% else %>
            <%= link_to polymorphic_path(feed.subject) do %>
              <%= render "/activities/project_record_feed", subject: feed.subject, :feed => feed %>
            <% end %>
          <% end %>
       <% end %>
    </div>

<% end %>

我收到错误消息 "undefined method `project_record_path' for #<#:0x007f9368c63f88>"

我知道路径不对,因为ProjectRecord属于Project。

我尝试用"project_project_record_path"代替"polymorphic_path(feed.subject)",但是找不到project_id和project_record的id。

new_project_project_record GET      /projects/:project_id/project_records/new(.:format)      project_records#new
edit_project_project_record GET      /projects/:project_id/project_records/:id/edit(.:format) project_records#edit
     project_project_record GET      /projects/:project_id/project_records/:id(.:format)      project_records#show

如果我删除 link_to 助手(project_record),则只有

  <%= render "/activities/project_record_feed", subject: feed.subject, :feed => feed %>

、dashboard/show 页面显示,其他 link 工作正常。

我希望每个 ProjectRecord 提要 link 到 ProjectRecords/show 页面。
如何进行这项工作?

要解决您的路由问题,您可以使用 shallow routes:

resources :projects do
  resources :project_records, shallow: true
end

这使得 index/new/create 操作嵌套在 Project 中,但 show/update/destroy 转到顶层,就好像资源没有嵌套一样。