如何使用私人提交活动Feed?

时间:2015-04-22 16:33:00

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

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

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

@valuations

在其中一个_forms中,例如@goals <%= 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 %> ,用户可以通过他提交的内容进行区分:

lv.setOnItemClickListener(new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> parent, View view,
            int position, long id) {

        String checkid = ((TextView) view.findViewById(R.id.id))
                .getText().toString();


        String checkimage = ((ImageView) view.findViewById(R.id.list_image)).getContext().toString();


        Intent in = new Intent(getApplicationContext(),
               UserProfile.class);

        in.putExtra("teste", checkid);
        in.putExtra("teste2", checkimage);

        startActivity(in);


    }
});

谢谢!

1 个答案:

答案 0 :(得分:0)

我会改用enum列。 Enums为您提供了大量功能,例如范围,询问甚至爆炸方法来更改状态。但是大多数枚举都是为了扩展而设计的 - 假设您想要添加用户可以拥有只能由朋友查看的帖子的功能 - 在枚举中添加其他状态很容易!

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

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

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

<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