如何使用public_activity gem(Rails)中的参数值? [英] How to use parameters value from public_activity gem (Rails)?

查看:99
本文介绍了如何使用public_activity gem(Rails)中的参数值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个Rails应用程序。在我的应用程序中,有些用户可以关注的项目。当用户关注其中一个页面时,如果有人上传/创建文件夹/文件,他/她会得到更新。



下面是截图,有人刚刚创建了一个新的文件夹:





在我的文件夹控制器中的Create动作的代码:

  def创建
@folder = current_user .folders.where(project_id:params [:project_id])。create(folder_params)

respond_to do | format |
if @ folder.save
@ folder.create_activity:create,owner:current_user,:params => {
:project_id => proc {|控制器,项目| @ folder.project.id},
:project_name => proc {|控制器,项目| @ folder.project.name},
}

format.html {redirect_to @ folder.project,注意:'文件夹已成功创建。'}
format.json {render :show,status::ok,location:@folder}
else
format.html {render:new}
format.json {render json:@ folder.errors,status::unprocessable_entity }
end
end

end

您可以看到 :project_id :project_name 是public_activity的参数一个新的文件夹被创建。



下面是关于这个参数值在数据库中保存后的样子的截图:





问题:



所以我的问题是,我如何在我的activities_controller中使用这些参数值?



以下是我的活动控制器的代码:

  class ActivitiesController< ApplicationController 
def index
@activities = PublicActivity :: Activity.order(created_at desc)。其中(owner_id:current_user.following_users,owner_type:User)
结束
结束

代替使用owner_id:,我想要使用参数栏中的project_id值。那么我该怎么做呢?



非常感谢您的高级! :)

解决方案

感谢您的回答,但我得到了比使用参数值或自定义字段更好的解决方案。
以下是我的activities_controller现在的样子:

  class ActivitiesController< ApplicationController 

def index
activity_table = PublicActivity :: Activity.arel_table

#我们希望查看所有与我们相关的文件夹活动
生成对与我们关心的文件夹相关的所有活动的查询
folders_query =文件夹信息文件夹folder_ids = Folder.where(project_id:current_user.following_projects.pluck(:id))。pluck activity_table [:trackable_type] .eq('Folder')。和(
activity_table [:trackable_id] .in(folder_ids)


#为所有用户生成查询,我们按照
users_query = activity_table [:owner_id] .in(current_user.following_users.pluck(:id))

activity_query = folders_query.or(users_query)
@activities = PublicActivity: :Activity.where(activity_query)
end
end

通过这种方式,我可以很容易地将用户和用户关注的项目中的活动结合起来。



您可以修改它以添加任何其他活动,例如从评论或投票中添加。



这将有助于其他人在那里使用public_activity宝石! :)

I am building a Rails app. And in my app, there are Projects where users can "Follow". When a user follows one of the pages, he/she will get updates if somebody uploads/creates a folder/file.

Below is the screenshot when somebody just created a new folder:

And below is the code for "Create" action in my Folder controller:

 def create
 @folder = current_user.folders.where(project_id: params[:project_id]).create(folder_params)

 respond_to do |format|
  if @folder.save
    @folder.create_activity :create, owner: current_user, :params => {
       :project_id => proc {|controller, project| @folder.project.id},
       :project_name => proc {|controller, project| @folder.project.name},
      }

    format.html { redirect_to @folder.project, notice: 'Folder was successfully created.' }
    format.json { render :show, status: :ok, location: @folder }
  else
    format.html { render :new }
    format.json { render json: @folder.errors, status: :unprocessable_entity }
  end
end

end

As you can see :project_id and :project_name are the parameters for the public_activity when a new folder being created.

And below is the screenshot on how this parameters value looks like in the database after they were saved:

QUESTION:

So my question is, how do i use this parameters values in my activities_controller?

Here is the code for my activities controller right now:

class ActivitiesController < ApplicationController
  def index
    @activities = PublicActivity::Activity.order("created_at desc").where(owner_id: current_user.following_users, owner_type: "User")
  end
end

Instead of using "owner_id:", I want to use the "project_id" value from parameters column. So how can i do this?

Thank you very much in advanced! :)

解决方案

Thanks for the answer, but I got a better solution than using the parameters value or custom field. Here is how my activities_controller looks like right now:

class ActivitiesController < ApplicationController

  def index
    activity_table = PublicActivity::Activity.arel_table

    # We want to view all activity of folders related to projects we are follwing
    folder_ids = Folder.where(project_id: current_user.following_projects.pluck(:id)).pluck(:id)

    # Generate query for all activity related to folders we care about
    folders_query = activity_table[:trackable_type].eq('Folder').and(
         activity_table[:trackable_id].in(folder_ids)
    )

    # Generate query for all users that we follow
    users_query = activity_table[:owner_id].in(current_user.following_users.pluck(:id))

    activity_query = folders_query.or(users_query)
    @activities = PublicActivity::Activity.where(activity_query)
  end
end

By using this way, I could easily combine the activities from the "Users" and also from the "Projects" that the user follows.

You can modify it to add any other activities such as from the "Comments" or "Voting".

Hope this will help other people out there that are using public_activity gem! :)

这篇关于如何使用public_activity gem(Rails)中的参数值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆