0

我在我的活动控制器中设置了这个

class ActivitiesController < ApplicationController
  def index
    following_ids = current_member.following_members.map(&:id)
    @activities = Activity.where("member_id in (?)", following_ids.push(current_member.id)).order("created_at desc")
  end
end

这在我的FollowsController 中设置:

def create
    @member = Member.find_by_user_name(params[:member_id])
    @follow_member = current_member.follow(@member)
    if @follow_member
        current_member.create_activity(@follow_member, 'followed')
        respond_to do |format|
          format.html { redirect_to @member }
          format.js 
        end
    end 
end

这设置为显示关注另一个成员的活动:

<div>
    <span class="status_name">
        <%= link_to activity.member.user_name, profile_path(activity.member) %>
    </span> 
    <span>
        is now following <%= link_to activity.targetable.following_member.user_name, "#" %>
    </span> 
    <span class="meta">
        <%= time_ago_in_words(activity.targetable.created_at) %> 
    </span>
</div>

<div class="act_content">
    <%= follow_profile_link activity.targetable.following_member %>
</div>

我能够执行以下操作以在提要中创建一个新的活动项目,但我不知道如何调用被关注的成员。我收到此错误:

undefined method `following_member' for #<Follow:0x835b150>

app/views/activities/follow/_followed.html.erb:3:in `_app_views_activities_follow__followed_html_erb__269116874_68857716'
app/views/activities/index.html.erb:14:in `block in _app_views_activities_index_html_erb___16061925_44845644'
app/views/activities/index.html.erb:5:in `_app_views_activities_index_html_erb___16061925_44845644'

我不知道我需要做什么或具体调用什么来完成这项工作。有任何想法吗?

编辑 - 模型

活动模型

class Activity < ActiveRecord::Base
  belongs_to :member
  belongs_to :targetable, polymorphic: true
end

跟随模型

class Follow < ActiveRecord::Base

  extend ActsAsFollower::FollowerLib
  extend ActsAsFollower::FollowScopes

  # NOTE: Follows belong to the "followable" interface, and also to followers
  belongs_to :followable, :polymorphic => true
  belongs_to :follower,   :polymorphic => true

  def block!
    self.update_attribute(:blocked, true)
  end

end

会员模型

class Member < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  # Setup accessible (or protected) attributes for your model
  attr_accessible :email, :email_confirmation, :password, :password_confirmation, :remember_me,
                :first_name, :last_name, :user_name, :pursuits, :avatar, :bio, :city, :state, :country, :pursuit_list

 has_many :medium
 has_many :statuses
 has_many :activities
 acts_as_follower
 acts_as_followable
 acts_as_ordered_taggable
 acts_as_ordered_taggable_on :pursuits

 def create_activity(item, action)
    activity = activities.new
    activity.targetable = item
    activity.action = action 
    activity.save 
    activity
 end 

编辑 - 活动迁移

class CreateActivities < ActiveRecord::Migration
  def change
    create_table :activities do |t|
        t.integer :member_id
        t.string :action

        t.integer :targetable_id
        t.string :targetable_type

        t.timestamps
    end

    add_index :activities, :member_id
    add_index :activities, [:targetable_id, :targetable_type]

  end
end

活动指数

<% @activities.each do |activity| %>
    <div class="status">
            <div class="row">
                <div class="span1 stream_av">
                    <%= avatar_profile_link activity.member %>
                </div> 
                <div class="span7">

                    <%= render partial: "activities/#{activity.targetable_type.underscore}/#{activity.action}",
                        locals: { activity: activity } %>

                </div>
            </div> 
        </div>
<% end %>

上传媒体的新活动

<div>
    <span class="status_name">
        <%= link_to activity.member.user_name, profile_path(activity.member) %></span>     <span>uploaded new <%= link_to "Media", profile_media_path(activity.member) %></span>     <span class="meta"> <%= time_ago_in_words(activity.targetable.created_at) %>
</span>
</div>

<div class="act_content">
    <%= link_to image_tag(activity.targetable.asset.url(:medium), class: ''), medium_path(activity.targetable_id) %>
</div>

写入状态的新活动

<div>
    <span class="status_name">
        <%= link_to activity.member.user_name, profile_path(activity.member) %></span>     <span>wrote a <%= link_to "Post", profile_stream_path(activity.member) %></span>     <span class="meta"> <%= time_ago_in_words(activity.targetable.created_at) %>
</span>
</div>

<div class="act_content">
    <%= activity.targetable.content %>
</div>
4

1 回答 1

0

following_member返回一个 ActiveRecord 对象。因此,您必须执行以下操作:

activity.targetable.following_member.first.user_name

代替

activity.targetable.following_member.user_name

如果要检索以下所有成员,则必须遍历它。

<%activity.targetable.following_member.each do |member| %>
  <%=member.user_name%>
<%end%>
于 2013-10-11T06:57:54.270 回答