我希望在 Rails 应用程序中的用户之间分享课程。用户 A 创建了一些课程并希望与用户 B 和 C 共享。用户 A 创建课程,将用户 B 和 C 添加到课程中,他们现在可以看到课程。我的问题是如何让共享课程出现在用户 B 和 C 共享页面中?每节课都属于一个笔记本。
笔记本.rb
belongs_to :user
has_many :lessons, :dependent => :destroy
课程.rb
belongs_to :notebook
has_many :shareships
has_many :users, through: :shareships, dependent: :destroy
attr_reader :user_tokens
accepts_nested_attributes_for :shareships, :reject_if => lambda { |a| a[:user_ids].blank? }
scope :shared, lambda { where('shared_ids = ?')
用户.rb
has_many :notebooks, dependent: :destroy
分享.rb
belongs_to :lesson
belongs_to :user
课程控制器.rb
class LessonsController < ApplicationController
before_filter :authorize, :find_notebook
load_and_authorize_resource :through => :notebook, :except => [:public]
respond_to :html, :js, :json
def create
@lesson = @notebook.lessons.build(params[:lesson])
@lesson.user_id = current_user.id
flash[:notice] = 'lesson Added!.' if @lesson.save
respond_with(@lesson, :location => notebook_lessons_path)
end
def shared
@user = current_user
@shared = @notebook.lessons
end
end
我已经在用户和课程之间设置了多对多关联,因此用户可以将其他用户添加到课程中,但我正在尝试弄清楚如何为共享用户列出课程。有什么想法可以让它发挥作用吗?我无法设置它以及我的控制器和视图。