我有三个模型用户,俱乐部和Mcq。
俱乐部的模特。我指派俱乐部(班)为-
这是我的协会
class User < ActiveRecord::Base
has_and_belongs_to_many :clubs
end
class Club < ActiveRecord::Base
has_many :mcqs
has_and_belongs_to_many :users
end
class Mcq < ActiveRecord::Base
belongs_to :club
end在我的学生观点(学生显示索引),我显示所有的俱乐部(作为主题)和点击后,我只想显示有关主题的Mcq主题。
所以我的俱乐部总监是-
class ClubsController < ApplicationController
#its show subject list
def student_show_index
@club = current_user.clubs
end
#its show topic according to subject.
def student_show_topic
@club = current_user.clubs
@mcq = @club.first.mcqs.order('created_at DESC')
end
end所以我的问题是,当我点击主题物理,它显示了所有的Mcq第9。化学也是一样。
我只想根据主题过滤Mcq。
发布于 2015-09-21 08:24:39
您必须在您正在单击的主题链接中发送一个params作为club_id。例如:然后,您可以将控制器操作中的这个参数捕获为<%=link_to "Subject", x_path(club_id: n) %>。然后重写控制器操作如下
def student_show_topic
@club = Club.find(params[:club_id])
@mcq = @club.mcqs.order('created_at DESC')
end如果还没有添加,您可能需要在控制器中允许club_id这个参数。希望这能帮到你。如果有什么问题请告诉我?
https://stackoverflow.com/questions/32690266
复制相似问题