如何使用私人提交来隐藏Feed?

时间:2015-04-04 02:36:13

标签: ruby-on-rails ruby feed railscasts public-activity

在评估表单中,有一个提交按钮和一个<%= f.submit :private %>按钮。如果单击私人提交,则提交的信息将隐藏给查看个人资料的其他用户。

我们如何使用<%= f.submit :private %>隐藏提交的信息以显示Feed?

活动/ index.html.erb

<h1>Feed</h1>
<% @activities.each do |activity| %>
<% if current_user == @user %>
    <%= render_activity activity %>
  <% else %>
    <%= render_activity activity %> #We'd need to make .public_valuations work with this without getting an undefined method error.
  <% end %>
<% end %>

activities_controller.rb

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

为简洁起见,我只会包含_create(还有updatedestroy)。每次用户提交评估时,它都会在Feed上弹出,我们怎么才能只显示public_valuations

public_activity /估值/ _create.html.erb

<% if activity.trackable %>
  <%= link_to activity.trackable.name, activity.trackable %></b>
<% else %>
  which has since been removed 
<% end %>

valuation.rb

class Valuation < ActiveRecord::Base
  belongs_to :user
  acts_as_taggable
  validates :name, presence: true
  has_many :comments, as: :commentable
  include PublicActivity::Model
  tracked owner: ->(controller, model) { controller && controller.current_user }

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

  scope :randomize, -> do
      order('RANDOM()').
      take(1)
    end
end

users_controller

 def show
   if
     @valuations = @user.valuations
   else
     @valuations = @user.public_valuations
   end
 end

user.rb

#gets public valutations or nil, if there's no public valutation
def public_valuations
  valuations.find(&:public?)
end

我从这个railscasts剧集中获得了几乎所有的活动代码:http://railscasts.com/episodes/406-public-activity

这就是我们如何为个人资料进行私人提交工作: How to use private submit to hide from profile?

非常感谢您的时间!

更新

由于我们无法通过public_activity gem解决问题,因此我从头开始创建公共活动,并尝试在此处解决此问题: How to make private activities?

1 个答案:

答案 0 :(得分:2)

FAILED ATTEMPT

at Avdept的建议:

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

valuations.rb

def public? !private end => def public?; !private; end;
scope :not_private, -> { where(private: false) }

这给出了:

SyntaxError in ActivitiesController#index (for line: def public? !private end => def public?; !private; end;)
/Users/galli01anthony/Desktop/Pecoce/app/models/valuation.rb:9: syntax error, unexpected '!', expecting ';' or '\n' def public? !private end => def public?; !private; end; ^ /Users/galli01anthony/Desktop/Pecoce/app/models/valuation.rb:9: syntax error, unexpected =>, expecting end-of-input def public? !private end => def public?; !private; end; ^