【问题标题】:How to use private submit with activities feed?如何将私人提交与活动提要一起使用?
【发布时间】:2015-06-30 11:24:32
【问题描述】:

我们如何为用户提供将活动设为私有的选项?这将为用户提供他们想要的帖子的隐私权。

我被告知此代码不起作用,因为它可能与 "not setting up the 'private' checkbox to work correctly" 有关,但私人复选框适用于 hiding submissions on the public profiles(只是不在活动提要上)。

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

@valuations@goals 等_forms 之一中,用户可以通过提交进行区分:

 <%= 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 %>

谢谢!

【问题讨论】:

  • 我对复选框的建议是基于没有真正看到所有代码的猜测。你说它隐藏了它们但不在提要上?哪些没有显示?你能去数据库中查找它们,看看它们的“私有”布尔值有什么价值吗?也许它们没有正确设置为“false”,而是 true 或 nil ......在这种情况下 - 是的,复选框有问题......你需要一个默认值。
  • 嘿,谢谢你在@TarynEast 见到我!我看到我们一直在编辑彼此的条目。我使用.public_activities 而不是.public 的原因是因为我收到了这个错误:You tried to define a scope named "public" on the model "Activity", but Active Record already defined a class method with the same name. 是我切换它错了还是你有更好的解决方案?我认为 Rails 4 他们保留了这个词。
  • 啊对...是的,这很公平。为其选择另一个名称 - 但最好避免“smurf 命名约定”(其中“Smurf”类具有“public_smurfs”方法);)
  • 抱歉,无法阅读……还有那些估值,而不是活动……我很困惑。还回顾以前的comment.s 你真的不需要这样做:def visible_valuations valuations.find(&amp;:visible?) end - 只需使用“可见”范围。 a_model.thingies.visible 应该可以工作,如果您有一个名为 visible 的范围,它将适用于任何集合 - 不仅适用于 Thingie 类,还适用于 has_many 集合。在这种情况下,说:user.valuations.visible
  • 感谢@TarynEast 您的最后评论!这帮助我回到了正确的轨道!我重命名和修改了很多,所以我认为最好从一个新问题开始:stackoverflow.com/questions/29834607/how-to-conceal-from-feed

标签: ruby-on-rails ruby model-view-controller feed


【解决方案1】:

我会改用enum 列。枚举为您提供了大量的功能,例如范围、询问甚至 bang 方法来更改状态。但是大多数枚举都是为扩展而构建的——假设你想添加用户可以拥有只有朋友可以查看的帖子的功能——向枚举添加额外的状态很容易!

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

rails g migration AddVisiblityToActivities visibility:integer:index

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

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

使用rake db:migrate 运行迁移。然后我们需要将枚举映射添加到 Activity 模型中:

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

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

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

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

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

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

# 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

【讨论】:

  • 这是完美的。谢谢你,马克斯。
猜你喜欢
  • 1970-01-01
  • 2013-07-19
  • 1970-01-01
  • 1970-01-01
  • 2015-04-10
  • 2015-06-02
  • 2021-02-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多