1

我想我可能会错过一些非常明显的东西,但我就是想不通。我正在尝试从 PublicActivity 上的 custom_activity 事件保存的参数中提取 id 和 provider 参数,并将它们传递给我的 Notification 模块并在视图中显示输出。我的问题是我无法从每个活动中获取需要传递给通知的信息。使用当前的设置,我可以获得所有活动,但我只能获得一组要传递的参数。所以.. 我如何设置每个活动 id 并从中获取所需的数据以传递给 Notification::Timeline?

before_action :set_activity, only: :show
def show
    @activities   = PublicActivity::Activity.order(created_at: :desc).where(recipient_id: current_user.id, recipient_type: 'User')
    @post_id      = set_activity.parameters[:id] 
    @provider     = set_activity.parameters[:provider]
    notify        = Notification::Timeline.new(@post_id, @provider, current_user)
    @post_entry   = notify.construct
    @post_entry
end
private

def set_activity
    PublicActivity::Activity.where(trackable_type: 'User').find(params[:id])
end

我的路线

resources :activities
get '/notifications/:id', to: 'activities#show'

为了清楚起见,这是一个单一的活动

<PublicActivity::Activity id: 13, trackable_id: 1, trackable_type: "User", owner_id: 1, owner_type: "User", key: "like", parameters: {:id=>"1187467057945665_1220996764592694", :provider=>"github"}, recipient_id: 1, recipient_type: "User", created_at: "2015-10-09 16:23:34", updated_at: "2015-10-09 16:23:34">

此外,我尝试直接提取数据,但我不确定如何将每个 id 和 provider 值与相应的活动匹配。

# @activities.where(trackable_type: 'User').pluck(:parameters).map do |id| {id: id[:id]} end
#  => [{:id=>"1187467057945665_1220988311260206"}, {:id=>"1231979956827708"}, {:id=>"1230071203685250"}, {:id=>"619281766889582596"}, {:id=>"1187467057945665_1220996764592694"}]
# @activities.where(trackable_type: 'User').pluck(:parameters).map do |provider| {provider: provider[:provider]} end
#  => [{:provider=>"github"}, {:provider=>"github"}, {:provider=>"github"}, {:provider=>"github"}, {:provider=>"stackoverflow"}]
4

1 回答 1

0

我想到了。我将其改回索引操作,只需在视图中设置变量

def index
    @activities = PublicActivity::Activity.order(created_at: :desc).where(recipient_id: current_user.id, recipient_type: 'User')
end

在我看来

<% @post_entry = Notification::Timeline.new(activity.parameters[:id], activity.parameters[:provider], activity.owner)%>
于 2015-10-11T22:04:17.710 回答