无法访问枚举器edit_multiple

时间:2012-07-18 17:40:54

标签: ruby-on-rails ruby

当我尝试在表单中编辑多个答案时,我收到以下错误,然后点击保存!

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

用户应该能够为同一项研究多次提交答案,并将答案保存为具有study_id,user_id和该研究中问题答案的study_idance。 在研究控制器中:

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和apoligize,但它让我很难过。

1 个答案:

答案 0 :(得分:0)

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