如何进行私人活动? [英] How to make private activities?

查看:88
本文介绍了如何进行私人活动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们如何为用户提供将活动设为私有的选项?这样一来,用户就可以只为自己想要的帖子提供隐私权.

How can we give the user the option to make activities private? This would give users privacy for posts they want for their eyes only.

这是我的尝试之一,这给了我

Here was one of my attempts, which gives me:

NoMethodError in ActivitiesController#index undefined method 'public_activities'用于行:@activities = Activity.public_activities.order("created_at desc").where(current_user.following_ids)

NoMethodError in ActivitiesController#index undefined method 'public_activities' for line: @activities = Activity.public_activities.order("created_at desc").where(current_user.following_ids)

class ActivitiesController < ApplicationController
  def index #Added .public_activities
    @activities = Activity.public_activities.order("created_at desc").where(user_id: current_user.following_ids)
  end
end


class Activity < ActiveRecord::Base
  belongs_to :user
  has_many :comments, as: :commentable
  belongs_to :trackable, polymorphic: true

  def public?
    !private
  end
end

create_table "activities", force: true do |t|
  t.boolean "private", default: false
  t.integer  "user_id"
  t.string   "action"
  t.integer  "trackable_id"
  t.string   "trackable_type"
  t.datetime "created_at",     null: false
  t.datetime "updated_at",     null: false
end

class User < ActiveRecord::Base
  has_many :activities
  def public_activities
    activities.find(&:public?)
  end
end

并且在_form之一(例如@valuations@goals)中,用户可以通过提交来进行区分:

And in one of the _forms, such as @valuations or @goals, is where the user can make the distinction via his submission:

 <%= button_tag(type: 'submit', class: "btn", id: "gold")  do %>
   <span class="glyphicon glyphicon-plus"></span> Public
 <% end %>

 <%= button_tag(type: 'submit', class: "btn") do %>
   <% :private %><span class="glyphicon glyphicon-plus"></span> Private
 <% end %>

许多代码都是从这里的答案中得到启发的:如何使用私人提交隐藏个人资料?

Much of this code was inspired from the answer here: How to use private submit to hide from profile?

谢谢!

推荐答案

class User < ActiveRecord::Base
  has_many :activities
  def public_activities
    activities.find(&:public?)
  end
end

这已经定义了一个名为public_activities的新 instance 方法-您将只能在用户实例上使用它

This has defined a new instance method called public_activities - you will only be able to use it on an instance of a user

class ActivitiesController < ApplicationController
  def index #Added .public_activities
    @activities = Activity.public_activities.order("created_at desc").where(current_user.following_ids)
  end
end

在这里,您尝试改为在Activity类上调用 class 方法.

Here you are trying to call a class method on the Activity class instead.

如果要执行上述操作,则需要在Activity类上创建一个作用域.

If you want to do the above, then you'll need to create a scope on the Activity class.

在这种情况下,最好不要重复名称中的活动"部分,而只需将其命名为公开"即可.

in which case, it's better not to repeat the "activities" part in the name, but just call it "public"

例如

class Activity < ActiveRecord::Base
  belongs_to :user
  has_many :comments, as: :commentable
  belongs_to :trackable, polymorphic: true

  scope :public, ->{ where(:private => false) }

  def public?
    private == true ? false : true
  end
end


class ActivitiesController < ApplicationController
  def index
    @activities = Activity.public.order("created_at desc").where(current_user.following_ids)
  end
end

这篇关于如何进行私人活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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