Ruby on rails 3 Rails3:嵌套形式不';无法保存,因为id为

Ruby on rails 3 Rails3:嵌套形式不';无法保存,因为id为,ruby-on-rails-3,forms,validation,nested-forms,nested-attributes,Ruby On Rails 3,Forms,Validation,Nested Forms,Nested Attributes,这两个模型(步骤包含“接受嵌套的属性”): 以及新视图中的嵌套表单,其中包含语句的form_字段: <%= form_for(@step) do |step_form| %> <div class="field"> <%= step_form.label :step_type %><br /> <%= select("step", "step_type_id", @step_types.collect {|p| [ p

这两个模型(步骤包含“接受嵌套的属性”):

以及新视图中的嵌套表单,其中包含语句的form_字段:

<%= form_for(@step) do |step_form| %>

   <div class="field">
    <%= step_form.label :step_type %><br />
    <%= select("step", "step_type_id", @step_types.collect {|p| [ p.name, p.id ] }, {:include_blank => true}) %>
  </div>

  <%= step_form.fields_for :statements do |statement_form| %>

  <div class="field">
    <%= statement_form.label :title %><br />
    <%= statement_form.text_field :title %>
  </div>

  <% end %>

  <div class="actions">
    <%= step_form.submit %>
  </div>
<% end %>


真})%%>

提交时,模型不会保存,因为:“语句步骤不能为空”(步骤应在…之前创建)

在保留验证的同时,您可以使用的反向\u进行此操作:

class Step < ActiveRecord::Base
  has_many :statements, inverse_of: :step
  accepts_nested_attributes_for :statements
end

class Statement < ActiveRecord::Base
  belongs_to :step, inverse_of: :statements
  validates :step, presence: true
end
类步骤
问题在于验证。我在语句模型中有这样一段代码:#validates validates:step_id,:presence=>true,带有一个所属对象。因此,我删除了验证,只是它正在工作。请回答您的问题并接受您自己的答案,这样它就不会显示为未解决。请把它作为答案贴出来。
<%= form_for(@step) do |step_form| %>

   <div class="field">
    <%= step_form.label :step_type %><br />
    <%= select("step", "step_type_id", @step_types.collect {|p| [ p.name, p.id ] }, {:include_blank => true}) %>
  </div>

  <%= step_form.fields_for :statements do |statement_form| %>

  <div class="field">
    <%= statement_form.label :title %><br />
    <%= statement_form.text_field :title %>
  </div>

  <% end %>

  <div class="actions">
    <%= step_form.submit %>
  </div>
<% end %>
class Step < ActiveRecord::Base
  has_many :statements, inverse_of: :step
  accepts_nested_attributes_for :statements
end

class Statement < ActiveRecord::Base
  belongs_to :step, inverse_of: :statements
  validates :step, presence: true
end