如何在 Rails 视图模板中摆脱这个令人讨厌的查询?
恶心。这让我很沮丧。
在我的控制器中:
@assessor = Assessor.find(params[:id])
@assessor.answers.build if @assessor.answers.empty?
在我看来:
= simple_form_for @assessor do |f|
- @assessor.candidates.each do |candidate|
- @assessor.assessment_competencies.each do |competency|
- if @assessor.answers.all?{|a| a.new_record?}
- competency.behaviors.each do |behavior|
= f.fields_for :answers do |f|
- @assessor.standard_answer_choices.each do |choice|
= f.input :assessor_id, :as => :hidden, :input_html => {:value => @assessor.id}
= f.input :candidate_id, :as => :hidden, :input_html => {:value => candidate.id}
= f.input :behavior_id, :as => :hidden, :input_html => {:value => behavior.id}
= f.input :competency_id, :as => :hidden, :input_html => {:value => competency.id}
= f.association :answer_choice, :collection => [choice], :as => :radio
- else
- competency.behaviors.each do |behavior|
- answer = Answer.find_or_create_by_behavior_id_and_assessor_id_and_candidate_id(behavior.id, @assessor.id, candidate.id)
= f.fields_for :answers, answer do |f|
= f.input :assessor_id, :as => :hidden, :input_html => {:value => @assessor.id}
= f.input :candidate_id, :as => :hidden, :input_html => {:value => candidate.id}
= f.input :behavior_id, :as => :hidden, :input_html => {:value => behavior.id}
= f.input :competency_id, :as => :hidden, :input_html => {:value => competency.id}
= f.association :answer_choice, :collection => [choice], :as => :radio
Yuck. It's bringing me down.
In my controller:
@assessor = Assessor.find(params[:id])
@assessor.answers.build if @assessor.answers.empty?
In my view:
= simple_form_for @assessor do |f|
- @assessor.candidates.each do |candidate|
- @assessor.assessment_competencies.each do |competency|
- if @assessor.answers.all?{|a| a.new_record?}
- competency.behaviors.each do |behavior|
= f.fields_for :answers do |f|
- @assessor.standard_answer_choices.each do |choice|
= f.input :assessor_id, :as => :hidden, :input_html => {:value => @assessor.id}
= f.input :candidate_id, :as => :hidden, :input_html => {:value => candidate.id}
= f.input :behavior_id, :as => :hidden, :input_html => {:value => behavior.id}
= f.input :competency_id, :as => :hidden, :input_html => {:value => competency.id}
= f.association :answer_choice, :collection => [choice], :as => :radio
- else
- competency.behaviors.each do |behavior|
- answer = Answer.find_or_create_by_behavior_id_and_assessor_id_and_candidate_id(behavior.id, @assessor.id, candidate.id)
= f.fields_for :answers, answer do |f|
= f.input :assessor_id, :as => :hidden, :input_html => {:value => @assessor.id}
= f.input :candidate_id, :as => :hidden, :input_html => {:value => candidate.id}
= f.input :behavior_id, :as => :hidden, :input_html => {:value => behavior.id}
= f.input :competency_id, :as => :hidden, :input_html => {:value => competency.id}
= f.association :answer_choice, :collection => [choice], :as => :radio
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
哦,那是一个粗糙的。
至少你可以将重复的
fields_for
块拉出到一个助手中:这会将你的视图缩减为这样:
如果你愿意,你可以将它分解为每个内部循环的助手,但是你明白了。
Oof, that is a gnarly one.
At the very least you can yank that repeated
fields_for
block out into a helper:That'll cut your view down to this:
If you wanted you could break it down into a helper for each inner loop, but you get the idea.