如何只在Ruby on Rails中显示公开帖子而不显示私有帖子?

时间:2015-06-06 14:28:55

标签: ruby-on-rails ruby ruby-on-rails-4

我有一个用户创建的集合(如果您愿意,也可以发布)。集合可以是公共的或私有的(在数据库中默认为true)。我想让用户看到他们所有的收藏品,但希望任何人能够看到只有收藏品才能公开。

我的意思是当任何人(用户或非用户)去查看收藏集(例如myapp.com/collections/some-collection)时,如果它是公开的,它会显示,但它是私有的,它不会显示并可能呈现“此收藏是私人的”并重定向。我可以使用一些帮助,谢谢!

分贝/ schema.rb

// get current date and time
$now = new DateTime();
// create DateTime object for purchase date
$purchaseDate = DateTime::createFromFormat('Y-m-d H:i:s', $row['purchasedate']);
// calculate seconds
$seconds = $now->getTimeStamp() - $purchaseDate->getTimeStamp();

应用/视图/集合/ show.html.erb

create_table "collections", force: :cascade do |t|
  t.string   "title"
  t.integer  "user_id"
  t.boolean  "display",     default: true
  t.text     "description"
end

应用/控制器/ collections_controller.rb

<div class="row">
  <div class="col-lg-10">
    <h2><%= best_in_place @collection, :title, as: :input %></h2>
    <p><%= best_in_place @collection, :description, as: :textarea %></p>
    <%= simple_form_for @collection do |f| %>
      <%= f.input :display, as: :boolean, checked_value: true, unchecked_value: false %>
      <%= f.button :submit %>
    <% end %>
  </div>
</div>

应用/模型/ collection.rb

def show
  @links = @collection.follows
  @collection = Collection.find_by_id(params[:id]) if params[:id].present?
  if params[:id].blank?
    @collections = Collection.user_collections(params.merge({"user_id" => current_user.id}))
  end   
end

2 个答案:

答案 0 :(得分:2)

before_filter :is_public?, only: [ :action_name]

def is_public?
  unless @collection.display?
    redirect_to :back
  end
end

答案 1 :(得分:0)

int i; for (i = 0; i < filelen - signlen; ++i) { if (memcmp(vir, fil + i, signlen) == 0) { return true; // vir exists in fil found } } return false; // vir is not in file 这应该返回所有可以显示的集合。