+1

Gem TheComments trong Rails

  Chào các bạn, hôm nay mình xin giơí thiệu một gem khá mạnh mẽ trong Rails để tạo chức năng comments. 

Đầu tiên ta cài đặt những gem cần dùng: mở Gemfile và thêm vào: gem "the_comments", "~> 2.2.1" gem 'haml' # or gem 'slim' gem 'awesome_nested_set' # or same gem Sau đó chạy bundle install để cài đặt gem Trong bài viết này mình sữ dụng gem the_comments cho bài post, và mình sẽ có model Post và User Chạy lệnh sau để tạo các bảng cần thiết: rake the_comments_engine:install:migratio Nó sẽ tạo ra cho mình 3 file trong db/migrate có tên là 20170122033608_the_comments_change_user.the_comments_engine.rb 20170122033609_the_comments_create_comments.the_comments_engine.rb 20170122033610_the_comments_change_commentable.the_comments_engine.rb Tiếp theo ta mở file 20170122033610_the_comments_change_commentable.the_comments_engine.rb và thêm vào như sau: class ChangeCommentable < ActiveRecord::Migration def change [:posts].each do |table_name| change_table table_name do |t| t.integer :draft_comments_count, default: 0 t.integer :published_comments_count, default: 0 t.integer :deleted_comments_count, default: 0 end end end end Tiếp theo mở file 20170122033608_the_comments_change_user.the_comments_engine.rb, bạn sẽ thấy rằng nó đã đươc thêm tự động vào users class TheCommentsChangeUser < ActiveRecord::Migration def change #if you User class is not called User, you may want to change it. change_table :users do |t|

      t.integer :my_draft_comments_count,      default: 0
      t.integer :my_published_comments_count,  default: 0
      t.integer :my_comments_count,            default: 0 # my_draft_comments_count + my_published_comments_count

      t.integer :draft_comcoms_count,     default: 0
      t.integer :published_comcoms_count, default: 0
      t.integer :deleted_comcoms_count,   default: 0
      t.integer :spam_comcoms_count,      default: 0
   end
end

end Tiếp theo chạy lệnh sau để cập nhật lại dữ liệu: rake db:migrate Bây giờ mình sẽ tạo file controller cho comments và những config cần thiết, bạn chạy lệnh sau: rails g the_comments install Nó sẽ tạo ra 3 file là: config/initializers/the_comments.rb app/controllers/comments_controller.rb app/models/comment.rb Tiếp theo mình mở file app/models/user.rb và thêm vào class User < ActiveRecord::Base include TheComments::User

  has_many :posts
  def admin?
    self == User.first
  end

  def comments_admin?
    admin?
  end

  def comments_moderator? comment
    id == comment.holder_id
  end

end Trong app/models/post.rb class Post < ActiveRecord::Base include TheComments::Commentable

 belongs_to :user
def commentable_title
  "Undefined Post Title"
end

def commentable_url
  "#"
end

def commentable_state
  "published"
end
end

Thêm vào routes file config/routes.rb MyApp::Application.routes.draw do root 'posts#index' resources :posts concern :user_comments, TheComments::UserRoutes.new concern :admin_comments, TheComments::AdminRoutes.new resources :comments, concerns: [:user_comments, :admin_comments] end Thêm đến app/controllers/application_controller.rb class ApplicationController < ActionController::Base include TheComments::ViewToken protect_from_forgery with: :exception end Thêm vào app/assets/stylesheets/application.css /* *= require the_comments */ Thêm vào app/assets/javascripts/application.js //= require the_comments Ví dụ code ở controller def show @post = Post.find params[:id] @comments = @post.comments.with_state([:draft, :published]) end Vi dụ code ở trang view app/views/posts/show.html.erb <%= render partial: 'the_comments/tree', locals: { commentable: @post, comments_tree: @comments }%>

Như vậy là mình đã hoàn thành xong chức năng comment cho bài post. Cảm ơn các bạn đã theo dõi. Link tham khảo: https://github.com/the-teacher/the_comments


All rights reserved

Viblo
Hãy đăng ký một tài khoản Viblo để nhận được nhiều bài viết thú vị hơn.
Đăng kí