0

当我尝试在表单中编辑多个答案并单击保存时出现以下错误!

TypeError (Cannot visit Enumerator):
  app/controllers/answers_controller.rb:61:in `edit_multiple'

一个用户应该能够为同一个研究多次提交答案,并且答案被保存为一个包含 study_id、user_id 和该研究中问题答案的 study_instance。在研究控制器中:

def group_answers
@study = Study.find_by_slug(params[:id])
@study_instance = StudyInstance.find(params[:study_instance])
@answers = @study_instance.answers
respond_to do |format|
  format.html 
  format.json { render :json => @study }
end
end

在答案控制器中,我有这个:

def edit_multiple
  @keys = params[:answer].keys.collect
  @answers = Answer.where(:id => @keys)
  @answers.each { |t| t.attributes = params[:answer][t.id.to_s] }
  if @answers.all?(&:valid?)
    @answers.each(&:save!)
    redirect_to studies_url, :notice => 'Study was successfully changed.'
  else
    return
  end
end

在 group_answers 视图中:

.block
  .content
    .inner
      %h2
        = @study.title
      %p.description
        = @study.description
        = @questions
= form_for :answers, :url => edit_multiple_answers_path, :html => { :multipart => true }     do |f|
  - @study_instance.answers.by_position.each do |answer|
    .block
      .content
        .inner
          %p
            = answer.position
            \.
            = answer.question.question_text
            %br/
            = fields_for "answer[#{answer.id}]" do |f|
              - if answer.question.long_text
                = f.text_area :answer_text, :value => answer.answer_text
              - elsif answer.question.short_text
                = f.text_field :answer_text, :value => answer.answer_text
              - elsif answer.question.file_upload
                = f.file_field :answer_image
                %br/
                %span.description 
                  Must be at least 500px x 500px and in gif or jpg format. 
                  %br/
                  No characters other than letters and numbers in file name.
                  %br/
                  Current_file:
                  %br/
                  = answer.answer_image_file_name ? (image_tag     answer.answer_image.url(:thumb)) : "none"
              - else
                = f.select :answer_text, answer.question.options_from_choices, :selected     => answer.answer_text, :include_blank => true

  .group.navform.wat-cf
    %button.button{:type => "submit"}
      = image_tag("web-app-theme/icons/tick.png", :alt => "#{t("web-app-theme.save",     :default => "Save")}")
      = t("web-app-theme.save", :default => "Save")
    %span.text_button_padding= t("web-app-theme.or", :default => "or")
    = link_to t("web-app-theme.cancel", :default => "Cancel"), studies_path, :class =>     "text_button_padding link_button"

如果这是非常简单的事情,我仍然是 Rails 的新手,但它让我感到难过。

4

1 回答 1

0

我发现你的代码有点奇怪......我认为最好使用嵌套属性。您可以在此处找到文档:http: //api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html和此处的一些示例:http ://railscasts.com/episodes?utf8=%E2%9C%93&search=嵌套的 .

于 2012-07-19T15:26:55.797 回答