如何在一个轨道中处理多个模型?

时间:2015-10-01 09:46:09

标签: ruby-on-rails ruby nested-forms

我有以下型号

import org.apache.commons.lang3.time.StopWatch;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class TimerInSeleniumWebDriver {
    public static void main(String[] args) {
        WebDriver driver;
        driver = new FirefoxDriver();       
        StopWatch pageLoad = new StopWatch();
        pageLoad.start();
        //Open your web app (In my case, I opened facebook)
        driver.get("https://www.facebook.com/");
        // Wait for the required any element (I am waiting for Login button in fb)
        WebDriverWait wait = new WebDriverWait(driver, 10);
        wait.until(ExpectedConditions.presenceOfElementLocated(By.id("u_0_l")));

        pageLoad.stop();
        //Get the time
        long pageLoadTime_ms = pageLoad.getTime();
        long pageLoadTime_Seconds = pageLoadTime_ms / 1000;
        System.out.println("Total Page Load Time: " + pageLoadTime_ms + " milliseconds");
        System.out.println("Total Page Load Time: " + pageLoadTime_Seconds + " seconds");
        driver.close();
    }
}

我的控制器:

class Survey < ActiveRecord::Base
  has_many :survey_sections
  accepts_nested_attributes_for :survey_sections
end

class SurveySection < ActiveRecord::Base
  belongs_to :survey
  has_many :questions
  accepts_nested_attributes_for :questions
end

class Question < ActiveRecord::Base
  belongs_to :survey_section
  has_many :answers
  belongs_to :question_group
  accepts_nested_attributes_for :question_group
  accepts_nested_attributes_for :answers
end

class Answer < ActiveRecord::Base
  belongs_to :question
end

class QuestionGroup < ActiveRecord::Base
  has_many :questions
end

如何在3个以上的模型中保存数据? 目前,我可以将调查表格数据保存到调查,调查部分和问题模型中。但我不知道控制器中有什么,我可以将数据保存到其他模型中。

2 个答案:

答案 0 :(得分:14)

如果正确使用fields_for帮助程序,则可以根据需要处理任意数量的表单。

我认为这是你失败的地方(你的控制器似乎没问题)。

我还有一段时间wrote an answer about this

#app/models/survey.rb
class Survey < ActiveRecord::Base
    has_many :sections
    accepts_nested_attributes_for :sections
end

#app/models/section.rb
class Section < ActiveRecord::Base
    belongs_to :survey
    has_many :questions
    accepts_nested_attributes_for :questions
end

#app/models/question.rb
class Question < ActiveRecord::Base
    belongs_to :section
    has_many :answers
end

尽量使您的模型名称保持简洁。

#app/controllers/surveys_controller.rb
class SurveysController < ApplicationController
   def new
      @survey = Survey.new
      @survey.sections.build.questions.build
   end

   def create
      @survey = Survey.new survey_params
      @survey.save
   end

   private 

   def survey_params
      params.require(:survey).permit(:title, sections_attributes: [:title, questions_attributes:[:title]])
   end
end

#app/views/surveys/new.html.erb
<%= form_for @survey do |f| %>
   <%= f.text_field :title %>
   <%= f.fields_for :sections do |section| %>
       <%= section.text_field :title %>
       <%= section.fields_for :questions do |question| %>
           <%= question.text_field :title %>
       <% end %>
   <% end %> 
   <%= f.submit %>
<% end %>

答案 1 :(得分:0)

您可以使用相同类型的模型获得最佳解释

http://railscasts.com/episodes/196-nested-model-form-part-1

#app/models/survey.rb
class Survey < ActiveRecord::Base
    has_many :sections, :dependent => :destroy
    accepts_nested_attributes_for :sections, :allow_destroy => true
end

#app/models/section.rb
class Section < ActiveRecord::Base
    belongs_to :survey
    has_many :questions, :dependent => :destroy
    accepts_nested_attributes_for :questions, :allow_destroy => true
end

#app/models/question.rb
class Question < ActiveRecord::Base
    belongs_to :section
    has_many :answers
end

现在在控制器

def new
  @survey = Survey.new
  section = @survey.sections.build
  section.questions.build 
  end
end

现在在视图中

<%= form_for @survey do |f| %>
  <%= f.error_messages %>
  <p>
    <%= f.label :name %><br />
    <%= f.text_field :name %>
  </p>
  <%= f.fields_for :sections do |builder| %>
     <%= builder.text_field :title %>
     <%= builder.fields_for :questions do |question| %>
        <%=  question.text_field :content%>
     <% end %>
  <% end %>
  <p><%= f.submit "Submit" %></p>
<% end %>