如何在 Rails 视图模板中摆脱这个令人讨厌的查询?

发布于 2024-12-06 20:51:49 字数 2015 浏览 1 评论 0原文

恶心。这让我很沮丧。

在我的控制器中:

@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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

椒妓 2024-12-13 20:51:50

哦,那是一个粗糙的。

至少你可以将重复的 fields_for 块拉出到一个助手中:

module AssessorsHelper
  def answers_fields f, candidate, behavior, competency, answer=nil
    assessor = f.object

    f.fields_for :answers, answer do |f|
      f.hidden_field :assessor_id,    :value => assessor.id
      f.hidden_field :candidate_id,   :value => candidate.id
      f.hidden_field :behavior_id,    :value => behavior.id
      f.hidden_field :competency_id,  :value => competency.id
      f.association :answer_choice, :collection => [choice], :as => :radio
    end
  end
end

这会将你的视图缩减为这样:

= 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|
          = answers_fields f, candidate, behavior, competency

      - else
        - competency.behaviors.each do |behavior|
          - answer = @assessor.answers.find_or_create_by_behavior_id_and_candidate_id behavior, candidate

          = answers_fields f, candidate, behavior, competency, answer

如果你愿意,你可以将它分解为每个内部循环的助手,但是你明白了。

Oof, that is a gnarly one.

At the very least you can yank that repeated fields_for block out into a helper:

module AssessorsHelper
  def answers_fields f, candidate, behavior, competency, answer=nil
    assessor = f.object

    f.fields_for :answers, answer do |f|
      f.hidden_field :assessor_id,    :value => assessor.id
      f.hidden_field :candidate_id,   :value => candidate.id
      f.hidden_field :behavior_id,    :value => behavior.id
      f.hidden_field :competency_id,  :value => competency.id
      f.association :answer_choice, :collection => [choice], :as => :radio
    end
  end
end

That'll cut your view down to this:

= 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|
          = answers_fields f, candidate, behavior, competency

      - else
        - competency.behaviors.each do |behavior|
          - answer = @assessor.answers.find_or_create_by_behavior_id_and_candidate_id behavior, candidate

          = answers_fields f, candidate, behavior, competency, answer

If you wanted you could break it down into a helper for each inner loop, but you get the idea.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文