English 中文(简体)
Nested object models, loops in view and erronous input makes views loop weirdly
原标题:

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.

问题回答

Try to render the partial with the subpath.

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




相关问题
rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

SSL slowness in EC2

We ve deployed our rails app to EC2. In our setup, we have two proxies on small instances behind round-robin DNS. These run nginx load balancers for a dynamically growing and shrinking farm of web ...

Auth-code with A-Za-z0-9 to use in an URL parameter

As part of a web application I need an auth-code to pass as a URL parameter. I am currently using (in Rails) : Digest::SHA1.hexdigest((object_id + rand(255)).to_s) Which provides long strings like : ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

activerecord has_many :through find with one sql call

I have a these 3 models: class User < ActiveRecord::Base has_many :permissions, :dependent => :destroy has_many :roles, :through => :permissions end class Permission < ActiveRecord::...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签