gpt4 book ai didi

ruby-on-rails - 如何使用私有(private)提交隐藏提要?

转载 作者:数据小太阳 更新时间:2023-10-29 07:00:35 26 4
gpt4 key购买 nike

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

我们怎样才能同时使用 <%= f.submit :private %>隐藏提交的信息以防止在提要上显示?

activities/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 )。每次用户提交估价时,它都会在提要上弹出,我们怎么能只做 public_valuations 显示?

public_activity/valuation/_create.html.erb

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

估值.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

用户 Controller

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

用户.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 .

这就是我们如何使个人资料的私有(private)提交工作: How to use private submit to hide from profile?

非常感谢您的宝贵时间!

更新

由于我们无法通过 public_activity gem 解决这个问题,我从头开始创建了公共(public)事件,并试图在这里解决这个问题: How to make private activities?

最佳答案

失败的尝试

根据 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; ^

关于ruby-on-rails - 如何使用私有(private)提交隐藏提要?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29442123/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com