undefined方法活动feed和acts_as_follower

时间:2013-10-11 05:54:12

标签: ruby-on-rails

我在 ActivitiesController中设置了这个:

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>

我能够得到以下操作来在Feed中创建一个新的活动项目,但我无法弄清楚如何打电话给被跟踪的成员。我收到这个错误:

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>

1 个答案:

答案 0 :(得分: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%>