0

我希望在 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

我已经在用户和课程之间设置了多对多关联,因此用户可以将其他用户添加到课程中,但我正在尝试弄清楚如何为共享用户列出课程。有什么想法可以让它发挥作用吗?我无法设置它以及我的控制器和视图。

4

1 回答 1

1

我会做这样的事情

笔记本.rb

has_many :lessons
belongs_to :user

课程.rb

belongs_to :notebook
has_many :shareships
has_many :users, through: :shareships, dependent: :destroy # shared users of this lesson, not the owner

用户.rb

has_many :notebook # the user will go through the notebook to get the lesson he owns
has_many :shareships
has_many :lessons, through: :shareships, dependent: :destroy # this would be only the shared lessons he has access to

分享.rb

belongs_to :lesson
belongs_to :user

用户可以通过以下方式访问他拥有的课程

/:user_id/notebooks/:notebook_id/lesson/:lesson_id 
# lessons = user.notebooks[:notebook_id].lessons

他可以通过以下方式获得与他分享的课程

/:user_id/shared_lessons/:lesson_id
# shared_lessons = user.lessons

用户无法直接访问他拥有的课程,他需要浏览他的笔记本。但他可以直接访问共享课程。

你怎么看 ?

于 2012-08-15T00:35:21.577 回答