假设我有元素哈希,我需要根据其中的类名过滤掉结果。
因此,如果记录包含PrivateSchool的元素,我可以将PrivateSchool记录传递给其各自的部分。
如果记录包含教师的元素,我可以将教师记录传递给其各自的部分。
所以,这是partails的代码:
<% if params[:tab] && params[:tab] == "featured" %>
<%= render partial: "featured", locals: {featured: @results} %>
<% elsif params[:tab] && params[:tab] == "schools" %>
<%= render partial: "schools", locals: {schools: @results} %>
<% elsif params[:tab] && params[:tab] == "playlists" %>
<%= render partial: "playlists", locals: {playlists: @results} %>
<% elsif params[:tab] && params[:tab] == "quizzes" %>
<%= render partial: "quizzes", locals: {quizzes: @results} %>
<% elsif params[:tab] && params[:tab] == "teachers" %>
<%= render partial: "teachers", locals: {teachers: @results} %>
<% end %>
如果我<%= @results.inspect%>
,则输出如下:
[#<LessonPlan id: 9, title: "advance java", description: "advance java",
notes: "advance java", subject_id: 2, teacher_id: 1, created_at: "2016-11-11
09:29:20", updated_at: "2016-11-11 09:29:20", is_private: true, is_publish:
false, user_id: 3, publish_date: nil, popularity_count: 0, is_special: false,
private_class_id: nil>, #<Quiz id: 1, title: "java history",
time_limit_in_minutes: 5, are_multiple_attempts_allowed: true, score_to_keep:
"highest", number_of_attempts_allowed: 4, student_see_quiz_score:
"after_each_attempt", show_one_question_at_a_time: true, is_private: false,
available_from_date_time: "2016-11-08 17:13:00", available_due_date_time:
"2016-11-10 17:18:00", instructions: "<p>java basics</p>", lesson_plan_id: 3,
user_id: 3, publish_date: "2016-11-09 12:14:04", created_at: "2016-11-09
12:14:04", updated_at: "2016-11-09 12:19:25", is_publish: true, subject_id: 3,
teacher_id: 1, popularity_count: 1, show_answers: "after_the_due_date">,
#<LessonPlan id: 4, title: "basics of java", description: "basics of java",
notes: "basics of java", subject_id: 2, teacher_id: 1, created_at: "2016-11-07
13:04:24", updated_at: "2016-11-10 10:40:54", is_private: false, is_publish:
false, user_id: 3, publish_date: "2016-11-09 08:08:41", popularity_count: 0,
is_special: false, private_class_id: nil>, #<Quiz id: 6, title: "java modern",
time_limit_in_minutes: 155, are_multiple_attempts_allowed: true,
score_to_keep: "latest", number_of_attempts_allowed: 9,
student_see_quiz_score: "after_last_attempt", show_one_question_at_a_time:
true, is_private: true, available_from_date_time: "2017-02-02 01:11:00",
available_due_date_time: "2017-02-27 01:11:00", instructions: "<p>java modern
java modern java modern java modern...", lesson_plan_id: 3, user_id: 3,
publish_date: "2016-12-22 20:37:35", created_at: "2016-12-22 20:37:35",
updated_at: "2017-02-09 20:11:38", is_publish: false, subject_id: 3,
teacher_id: 1, popularity_count: 0, show_answers: "after_the_due_date">,
#<Quiz id: 4, title: "Java quiz v1", time_limit_in_minutes: 180,
are_multiple_attempts_allowed: true, score_to_keep: "latest",
number_of_attempts_allowed: 6, student_see_quiz_score: "after_last_attempt",
show_one_question_at_a_time: false, is_private: false,
available_from_date_time: "2016-11-21 12:48:00", available_due_date_time:
"2016-11-23 12:48:00", instructions: "<p>Java quiz v1</p>\r\n<p
style=\"margin: 0px;\"><!--E...", lesson_plan_id: 8, user_id: 3, publish_date:
"2016-11-14 07:18:41", created_at: "2016-11-14 07:18:41", updated_at:
"2016-12-11 14:21:46", is_publish: false, subject_id: 3, teacher_id: 1,
popularity_count: 0, show_answers: "after_the_quiz">, #<Quiz id: 3, title:
"java basics", time_limit_in_minutes: 4, are_multiple_attempts_allowed: true,
score_to_keep: "highest", number_of_attempts_allowed: 10,
student_see_quiz_score: "after_last_attempt", show_one_question_at_a_time:
true, is_private: false, available_from_date_time: "2017-01-31 14:13:00",
available_due_date_time: "2017-02-17 14:13:00", instructions:
"<p>XXXXXXXXXXQQQQ</p>", lesson_plan_id: 8, user_id: 3, publish_date:
"2016-11-10 20:27:32", created_at: "2016-11-10 20:27:31", updated_at:
"2017-02-01 09:13:22", is_publish: false, subject_id: 3, teacher_id: 1,
popularity_count: 1, show_answers: "after_the_quiz">, #<PrivateSchool id: 18,
teacher_id: 39, user_id: 47, title: "Dr. Jasmine E McNair High School",
short_description: "Dr. Jasmine E McNair High School", price_type: "paid",
logo: "download.jpg", slug: "dr-jasmine-e-mcnair-high-school", created_at:
"2016-12-13 20:18:28", updated_at: "2016-12-13 20:18:28">]
我真正想要的是按照模型类名过滤这些记录,这样我就可以过滤记录并将相应的记录发送到它们的部分,而不是仅在每个部分中显示所有记录。
目前它显示一个部分错误的所有记录我需要按部分过滤它们,所以每个部分都会显示他的结果而不是其他模型。
所以, 如果记录属于LessonPlan,则LessonPlan中的所有记录都将部署在课程计划中。
如果记录属于教师,那么来自教师的所有记录都将归入教师部分。 等等。
这是我的要求。
答案 0 :(得分:4)
你可以尝试这样的事情:
hash = @results.group_by(&:class)
然后:
hash[LessonPlan] will give you a list of all the LessonPlan
修改:
<%= render partial: "quizzes", locals: {quizzes: hash[Quiz]} %>
说明:
第一步。我们根据对象的类创建Hash。为了实现这一点,我们使用Ruby方法&#39; group_by&#39;对于可数。
@ results.group_by(&amp;:class)相当于@ results.group_by {| result |结果[:类]}
然后我们可以通过类名轻松访问每个哈希的值。 hash [Quiz]将返回所有Quiz对象。
答案 1 :(得分:1)
可以使用select方法过滤结果:
@results.select { |r| r.is_a?(LessonPlan) } # etc.
因此,您的观点可能如下所示:
<%= render partial: "lesson_plans", locals: {
lesson_plans: @results.select { |r| r.is_a?(LessonPlan) }
} %>