【发布时间】: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(&: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