调查应用程序使用嵌套模型,部分和& Rails表单生成器

时间:2015-08-18 15:30:59

标签: ruby-on-rails nested-forms formbuilder

我从以下引擎中提取了此代码:

演示链接: https://rapidfire.herokuapp.com/rapidfire/question_groups/1110/answer_groups/new

指向代码的链接: https://github.com/code-mancers/rapidfire/blob/master/app/services/rapidfire/answer_group_builder.rb

我尝试修改它,以便我可以创建自定义答案类型,称为" kind"(s)(参见架构)。一种称为"指标" (参见_indicator.html.erb),有多列。

我的模型设置如下:

Survey
  has_many :question_groups
  accepts_nested_attributes_for :question_groups
end

QuestionGroup
  belongs_to :survey
  has_many :questions
  accepts_nested_attributes_for :questions
end

AnswerGroup
  belongs_to :survey
  has_many :answers
  accepts_nested_attributes_for :answers
end

Answer
  belongs_to :question
  belongs_to :answer_group, inverse_of: :answers
end

问题1:此行有什么作用?(请参阅build_answer_group方法)

@answers = question_group.questions.collect do |question|

在控制台中,

@answers = [#<Answer id: nil, answer_group_id: nil, question_id: 1034,
answer_text: nil, created_at: nil, updated_at: nil>, #<Answer id: nil,
answer_group_id: nil, question_id: 1035, answer_text: nil, created_at: nil,
updated_at: nil>]

question_ids 1034和1035指的是由

生成的数组中的最后一项
question_group.questions

相反,我需要@answers作为所有问题的答案类。 为什么它只返回两个项目?

问题2: 对于指标部分,我需要显示问题组的名称和每个子项的名称,例如&#34;问题&#34;,以及五个具有唯一html名称和ID的其他列。所以,如果我有两个类型的问题组&#34;指示符&#34;,我需要产生类似的东西:

QuestionGroup1.name
id = answer_group_1034_answer_text name = answer_group[1034][answer_text]
id = answer_group_1035_answer_text name = answer_group[1035][answer_text]
etc...

QuestionGroup2.name
id = answer_group_1036_answer_text name = answer_group[1036][answer_text]
id = answer_group_1037_answer_text name = answer_group[1037][answer_text]

目前正在制作:

For QuestionGroup1:

<textarea rows="5" name="answer_group[1034][answer_text]"
id="answer_group_1034_answer_text"></textarea>
<textarea rows="5" name="answer_group[1034][answer_text]"
id="answer_group_1034_answer_text"></textarea>


For QuestionGroup2:

<textarea rows="5" name="answer_group[1035][answer_text]"
id="answer_group_1034_answer_text"></textarea>
<textarea rows="5" name="answer_group[1035][answer_text]"
id="answer_group_1034_answer_text"></textarea>

目标:为问题组中的每一行指定唯一ID和名称

视图/ answer_groups / _fidelitychecklist.html.erb

<%= form_for([@survey, @answer_group_builder]) do |f| %>
   <% @answer_group_builder.answers.each do |answer| %>
     <%= f.fields_for("#{answer.question.id}", answer) do |answer_form| %>
       <%= render_answer_form_helper(answer, answer_form) %>
     <% end %>
   <% end %>
   <%= f.submit "Save" %>
<% end %>

服务/ answer_group_builder.rb

class AnswerGroupBuilder < BaseService
  attr_accessor :user, :survey, :questions, :answers, :params

  def initialize(params = {})
   super(params)
   build_answer_group
  end

  def to_model
   @answer_group
  end

  ...

  private
  def build_answer_group
    @answer_group = AnswerGroup.new(user: user, survey: survey)

    @survey.question_groups.each do |question_group|
      @answers = question_group.questions.collect do |question|
        @answer_group.answers.build(question_id: question.id)
      end
    end
  end

AnswerGroupsController

before_filter :find_survey!

def new
  @answer_group_builder = AnswerGroupBuilder.new(answer_group_params)
end

def create
  @answer_group_builder = AnswerGroupBuilder.new(answer_group_params)

  if @answer_group_builder.save
    redirect_to surveys_path
  else
    render :new
  end
end

private
def find_survey!
  @survey = Survey.find(params[:survey_id])
end

def answer_group_params
  answer_params = { params: params[:answer_group] }
  answer_params.merge(user: current_user, survey: @survey)
end

模块ApplicationHelper

def render_answer_form_helper(answer, form)
  partial = Question.where(id: answer.question_id).last.question_group.kind.to_s
  questiongroup = Question.find(answer.question_id).question_group
  render partial: "/answers/#{partial}", locals: {f: form, answer: answer, questiongroup: questiongroup }
end

视图/答案/ _indicator.html.erb

<%= render partial: "answers/errors", locals: {answer: answer} %>

<fieldset>
  <table class="indicator">
    <thead>
      <th class="first head-row">Indicators</th>
      <th class="head-row">Presence</th>
      <th class="head-row">Rating</th>
      <th class="head-row">Element(s) Implemented Well</th>
      <th class="head-row">Element(s) Requiring Further Support</th>
      <th class="head-row">Comments/Action Steps</th>
    </thead>
    <tbody>
      <tr><td colspan="6" class="indicator-title"><%= questiongroup.name %></td></tr>

      <% questiongroup.questions.each do |question| %>

      <tr>
        <td class="first row">
            <%= question.question_text %>
        </td>
        <td class="row">
            <%= f.select :answer_text, options_for_select([["N/O", "n/o"], ["N", "n"]]) %>
        </td>
        <td class="row">
            <%= f.select :answer_text, options_for_select([["1", "1"], ["2", "2"], ["3", "3"]]) %>
        </td>
        <td class="row">
            <%= f.text_area :answer_text, rows: 5 %>
        </td>
        <td class="row">
            <%= f.text_area :answer_text, rows: 5 %>
        </td>
        <td class="row">
            <%= f.text_area :answer_text, rows: 5 %>
        </td>
      </tr>
    <% end %>
    </tbody>
  </table>
</fieldset>

schema.rb

create_table "surveys", force: :cascade do |t|
  t.string   "name",            limit: 255
end

create_table "question_groups", force: :cascade do |t|
  t.string   "name",       limit: 255
  t.integer  "position",   limit: 4
  t.integer  "survey_id",  limit: 4
  t.text     "kind",       limit: 65535
end

create_table "questions", force: :cascade do |t|
  t.string   "question_text",     limit: 255
  t.integer  "position",          limit: 4
  t.integer  "question_group_id", limit: 4
end

create_table "answer_groups", force: :cascade do |t|
  t.integer  "survey_id",  limit: 4
end

create_table "answers", force: :cascade do |t|
  t.integer  "answer_group_id", limit: 4
  t.integer  "question_id",     limit: 4
  t.text     "answer_text",     limit: 65535
end

的routes.rb

resources :surveys do
  resources :answer_groups, only: [:new, :create] do
  end
end

0 个答案:

没有答案