首页 文章

如何显示仅连接到单个用户的记录

提问于
浏览
0

我希望你能在这里帮助我 . 非常感谢

我希望用户只能看到他们分配到的课程的指定 . 如何在控制器中进行设置?

我有3个型号:

class Appointment <ActiveRecord :: Base

belongs_to:用户
belongs_to:当然

结束

class User <ActiveRecord :: Base

has_many:courses,:through =>:assignments
has_many:约会

结束

课程<ActiveRecord :: Base

has_many:users,:through =>:assignments
has_many:约会

结束

2 回答

  • 1

    你应该能够做到:

    @appointments = current_user.appointments
    

    或者我在你的问题中遗漏了什么?


    编辑:一个更详细的例子:

    在你的控制器中(假设它是AppointmentsController):

    class AppointmentsController < ApplicationController
    
      def index
        @appointments = current_user.appointments
      end
    
    end
    

    然后,在相应的视图中(appointmentments / index.html.erb):

    <%- @appointments.each do |appointment| -%>
      <%= appointment.name %>
    <%- end -%>
    

    这完全基于你有current_user可用的假设,但由于你用'devise'标记了这个问题,我假设你这样做了 .

  • 0

    我想补充一下idlefingers的答案,你应该重新检查课程和用户之间的关系 . 看起来有点奇怪,双方都很好 . 使用has_many_and_belongs_to

相关问题