如何将私人提交与活动供稿一起使用? [英] How to use private submit with activities feed?

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

问题描述

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

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.

有人告诉我该代码不起作用,因为它可能与,但私有复选框适用于

I was told this code isn't working because it might have something to do with "not setting up the 'private' checkbox to work correctly", yet the private checkbox works for hiding submissions on the public profiles (just not on the activities feed).

class ActivitiesController < ApplicationController
  def index #Added .public
    @activities = Activity.visible.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
  scope :visible, ->{ where(:hidden => false) }

  def visible?
    !hidden
  end
end

create_table "activities", force: true do |t|
  t.boolean "hidden", 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

并且在_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 %>
   <% :hidden %><span class="glyphicon glyphicon-plus"></span> Private
 <% end %>

谢谢!

推荐答案

我将使用枚举列.枚举为您提供了很多功能,例如范围,查询甚至是改变状态的爆炸方法.但是大多数枚举都是为了扩展而设计的-假设您想添加用户可以拥有只能由朋友查看的帖子的功能-向枚举添加其他状态很容易!

I would use an enum column instead. Enums give you tons of functionality such as scopes, interrogation and even bang methods to change the status. But most of all enums are built to extend - lets say you want to add the functionality that users can have posts that are only viewable by friends - adding an additional state to an enum is easy!

首先,我们添加一个数据库列.运行:

First we add a database column. Run:

rails g migration AddVisiblityToActivities visibility:integer:index

然后编辑迁移以添加默认设置:

Then edit the migration to add a default:

class AddVisibilityToActivities < ActiveRecord::Migration
  def change
    t.integer :visibility, index: true, default: 0
  end
end

使用rake db:migrate运行迁移.然后,我们需要将枚举映射添加到活动"模型中:

Run the migration with rake db:migrate. Then we need to add the enum mappings to the Activity model:

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

  # change the order if you want to default to private!
  enum visibility: [:visible, :hidden]

  default_scope { visible.order('created_at DESC') }
end

请注意,我们还添加了默认范围.这样,我们可以真正简化控制器中的查询:

Note that we also add a default scope. With that we can really simplify the query in our controller:

class ActivitiesController < ApplicationController
  def index #Added .public
    @activities = Activity.where(user: current_user.following)
    # note that you don't have to use ids when creating a
    # where clause from an association. Rails does the work for you
  end
end

让用户在创建/更新记录时更改可见性的最简单方法是使用选择:

The easiest way to let users alter the visibility when creating/updating records is to use a select:

<div class="field">
  <%= f.label :visibility %>
  <%= f.select :visibility, Activity.visibilities.keys.map(&:titleize) %>
</div>

只需记住将visibility属性列入白名单!

Just remember to whitelist the visibility property!

# app/controllers/activities_controller.rb

# ...

def create
  @activity = Activity.new(activity_params) do |a|
    a.user = current_user
  end

  # ...
end

# ...

def activity_params
  params.require(:activity).permit(:visibility)
end

这篇关于如何将私人提交与活动供稿一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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