Hello! I've been googling and testing on this problem for quite a while, but can't find an answer.
I have a site, that servers surveys uploaded by admins. These surveys belong to surveycycles and answers belong to answerings.
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
The Controller is quite basic for nested objects
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
Everything inside an if, because render :action => "new" seems to use the objects in 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"
The views I handle with partials like so
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 %>
The stuff in the view partials is pretty bloated, since the questions can be from a wide variety
Now about the prolem! When the user submits this form it obviously renders the form again(I force the answering controllers create-action to 'render :action => "new" for debugging this problem I keep having). Instead of getting the same form again, the questions are rendered x times more, where with 8 questions x is 9. For example with a short surveycycle with 8 questions, after the first submit each question is display 9 times. Looking at the page source, there are on the first submit input id:s from 0 to 8, then 0 to 80 and then 0 to 728 suddenly.
I'll illustrate this:
..surveystuff..
Q: I was immersed in the game
Very immersed o o x o o o o not at all immersed
..surveystuff..
After user clicks submit, this becomes:
..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..
Could this be a bug or something, or am I doing something wrong?
I can post more stuff if needed.