如何编写简单的if语句来检查当前用户是否已经查看过某些内容?

时间:2018-10-07 02:01:31

标签: ruby-on-rails if-statement user-defined-functions

该视图中我已尝试了所有方法。目前,我有:

发表评论

  <% if current_user.reviews.include?(@book) %>
  <p>You've already reviewd this</p>
  <% else %>
    <div id="review-links">
      <%= link_to "write a review", new_book_review_path(@book.id) %>
    </div>
  <% end %>
<% end %>

user.rb

class User < ApplicationRecord
mount_uploader :avatar, AvatarUploader

  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :validatable
  has_many :books, dependent: :destroy
  has_many :reviews , dependent: :destroy
  enum access_level: [:author, :reviewer]

  has_and_belongs_to_many :claims, join_table: :books_users, association_foreign_key: :book_id, class_name: Book.to_s

def full_name
  first_name + " " + last_name
end

end

book.rb

class Book < ApplicationRecord
  mount_uploader :avatar, AvatarUploader
  belongs_to :user
  has_many :reviews
  has_and_belongs_to_many :genres, dependent: :destroy
  has_and_belongs_to_many :claims, join_table: :books_users, association_foreign_key: :user_id, class_name: User.to_s


end

review.rb

class Review < ApplicationRecord
  belongs_to :user
  belongs_to :book
end

schema.rb

create_table "reviews", force: :cascade do |t|
    t.text "comment"
    t.integer "rating"
    t.integer "user_id"
    t.integer "book_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["book_id"], name: "index_reviews_on_book_id"
    t.index ["user_id"], name: "index_reviews_on_user_id"
  end

很遗憾,即使审阅者已经为该书撰写了一篇评论,我仍然可以在该书的显示页面上显示评论链接。我究竟做错了什么?

0 个答案:

没有答案