この問題についてかなり長い間グーグルでテストしてきましたが、答えが見つかりません。
管理者がアップロードした調査をサーバーするサイトがあります。これらの調査は調査サイクルに属し、回答は回答に属します。
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
render :action => "new" は create でオブジェクトを使用しているように見えるため、if 内のすべて。
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 %>
質問は多種多様なものになる可能性があるため、ビューパーシャルの内容はかなり肥大化しています
さて、問題について!ユーザーがこのフォームを送信すると、明らかにフォームが再度レンダリングされます(私が抱えているこの問題をデバッグするために、応答するコントローラーの create-action に 'render :action => "new" を強制します)。同じフォームを再度取得する代わりに、質問は x 倍多くレンダリングされます。ここで、8 つの質問の場合、x は 9 です。たとえば、8 つの質問を含む短いアンケート サイクルでは、最初の送信後に各質問が 9 回表示されます。ページのソースを見ると、最初の送信入力 ID に 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..
これはバグか何かでしょうか、それとも私が何か間違ったことをしているでしょうか?
必要に応じて、さらに多くのものを投稿できます。