2009-11-17 35 views
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 

一切很基本的內部的,如果,因爲渲染:行動=>「新」似乎使用的對象創建。

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" 

我用諧音處理類似的意見,使

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 %> 

在視圖中諧音的東西是相當臃腫,因爲這些問題可以從多種

現在關於該prolem!當用戶提交這個表單時,它顯然會再次呈現表單(我強制回答控制器的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.. 

難道這是一個錯誤什麼的,還是我做錯了什麼?

如果需要,我可以發佈更多的東西。

+0

mmmm ...更多的東西是絕對不需要的,呵呵,但我確實很難理解最後幾段。你可以試着重新說一下嗎? – btelles 2009-11-18 06:08:08

+0

好的,我添加了一些插圖,並試圖添加一些感覺:p這是有點遲了,當我寫這 – Hopeanuoli 2009-11-18 07:04:31

回答

0

嘗試渲染部分子路徑。

<%= render :partial => 'answers/answer', :locals => {:f => f, :question => question} %> 
+0

這不起作用,因爲它只是尋找app/views/answers/_answer.html.erb而不是應用程序/views/answerings/_answer.html.erb。經過測試,它沒有改變。 – Hopeanuoli 2009-11-26 12:07:52