0

我一直在谷歌搜索和测试这个问题很长一段时间,但找不到答案。

我有一个网站,服务器调查由管理员上传。这些调查属于调查周期,答案属于答案。

class Answering < ActiveRecord::Base
  has_many :answers
  accepts_nested_attributes_for :answers, :allow_destroy => true
  has_many :alternative_answers, :through => :answers
  accepts_nested_attributes_for :alternative_answers, :allow_destroy => true
  validate :check_for_fail
end

class Answer < ActiveRecord::Base
 belongs_to :answering
 has_many :alternative_answers, :dependent => :destroy
 accepts_nested_attributes_for :alternative_answers

控制器对于嵌套对象来说是非常基础的

def new
 if @surveycycle.nil?
   @surveycycle = Surveycycle.find(params[:surveycycle_id])
   if !params[:game_id].nil?
     @game = Game.find(params[:game_id])
     @game_id = @game.id
   else
     @game_id = nil
   end
   if @answering.nil?
     @answering = Answering.new()
     @answering.answers.build
     @answering.answers.each do |x|
       x.alternative_answers.build
     end
   end
   @surveys = []
   for survey in @surveycycle.surveys
     @surveys << Survey.find(survey.id, :include => [:sub_surveys, :questions])
   end
 end
end

if 中的所有内容,因为 render :action => "new" 似乎使用了 create 中的对象。

def create
@surveycycle = Surveycycle.find(params["answering"].delete(:surveycycle_id).to_i)
if !params[:answering][:game_id].nil? and !params[:answering][:game_id].empty?
  @game = Game.find(params[:answering][:game_id])
  @game_id = @game.id
else
  @game = nil
  @game_id = nil
end

#If user is not logged in, use a fakeuser
if !logged_in?
  @fakeuser = Fakeuser.new
  @fakeuser.save
  @answering = Answering.new(params[:answering].merge({:fakeuser_id => @fakeuser.id}))
else
  @answering = Answering.new(params[:answering])
end

@surveys = []
for survey in @surveycycle.surveys
  @surveys << Survey.find(survey.id, :include => [:sub_surveys, :questions])
end
#Dummy version for debugging
render :action => "new"

我用这样的部分处理的视图

新的.html.erb

<% form_for :answering, @answering, :url => {:action => "create"}, :html => {:method => :post} do |all_f| %>
 <%= all_f.error_messages %>
 <%= all_f.hidden_field :game_id, :value => @game_id %>
 <% if logged_in? %>
   <%= all_f.hidden_field :user_id, :value => current_user.id %>
 <% end %>
 <%= all_f.hidden_field :surveycycle_id, :value => @surveycycle.id %>
 <% @surveys.each_with_index do |survey, survey_index| %>
  <div id="whole_survey">
    <% survey.sub_surveys.each_with_index do |sub_survey, sub_survey_index| %>
      <div id="sub_survey">
        <b><div id="sub_survey_name"><%= sub_survey.name %> </div></b><br>
        <% sub_survey.questions.each_with_index do |question, question_index| %>
          <table>
            <div id="question">
              <% all_f.fields_for :answers do |f| %>
                <%= render :partial => 'answer', :locals => {:f => f, :question => question} %>
              <% end %>
            </div>
          </table>
        <% end %>
      </div>
    <% end %>
  </div>
 <% end %>
 <%= all_f.submit t('answer') %>
<% end %>

视图部分中的内容非常臃肿,因为问题可能来自各种各样

现在关于这个问题!当用户提交此表单时,它显然会再次呈现表单(我强制应答控制器 create-action 'render :action => "new" 以调试我一直遇到的这个问题)。不是再次获得相同的表格,而是将问题呈现 x 次,其中 8 个问题 x 为 9。例如,对于 8 个问题的短调查周期,在第一次提交后,每个问题显示 9 次。查看页面源码,第一次提交时输入id:s从0到8,然后0到80,然后突然0到728。

我将说明这一点:

  ..surveystuff..
             Q: I was immersed in the game
    Very immersed o o x o o o o not at all immersed
  ..surveystuff..

用户点击提交后,变成:

  ..surveystuff..
             Q: I was immersed in the game
             Q: I was immersed in the game
             Q: I was immersed in the game
             Q: I was immersed in the game
             Q: I was immersed in the game
             Q: I was immersed in the game
             Q: I was immersed in the game
             Q: I was immersed in the game
             Q: I was immersed in the game
             Q: I was immersed in the game
    Very immersed o o x o o o o not at all immersed
    Very immersed o o o o o o o not at all immersed
    Very immersed o o o o o o o not at all immersed
    Very immersed o o o o o o o not at all immersed
    Very immersed o o o o o o o not at all immersed
    Very immersed o o o o o o o not at all immersed
    Very immersed o o o o o o o not at all immersed
    Very immersed o o o o o o o not at all immersed
    Very immersed o o o o o o o not at all immersed
    Very immersed o o o o o o o not at all immersed
  ..surveystuff..

这可能是一个错误或什么,还是我做错了什么?

如果需要,我可以发布更多内容。

4

1 回答 1

0

尝试使用子路径渲染部分。

<%= render :partial => 'answers/answer', :locals => {:f => f, :question => question} %>
于 2009-11-25T16:09:36.037 回答