Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如何为表单中的嵌套模型创建关联_Ruby On Rails - Fatal编程技术网

Ruby on rails 如何为表单中的嵌套模型创建关联

Ruby on rails 如何为表单中的嵌套模型创建关联,ruby-on-rails,Ruby On Rails,在我的RubyonRails应用程序中,我希望允许添加/编辑一个嵌套模型,该模型本身有一个关联的模型 model Survey string title has_many questions model Question string question belongs_to category model Category string name 为了便于讨论,让我们假设用户在输入问题时总是必须输入一个新类别(我想不出更好的例子,叹气) 在我的model/sur

在我的RubyonRails应用程序中,我希望允许添加/编辑一个嵌套模型,该模型本身有一个关联的模型

model Survey
   string title
   has_many questions

model Question
   string question
   belongs_to category

model Category
   string name
为了便于讨论,让我们假设用户在输入问题时总是必须输入一个新类别(我想不出更好的例子,叹气)

在我的model/survey/edit.html.erb中,我有一个用于添加问题和保存问题的工作设置。但是,当我将
类别
模型添加到图片中时,我现在面临的问题是,当添加新的
问题
时,没有显示相应的
类别
名称字段。我怀疑这是因为即使我调用Question.new,我也不调用Question.category.build-我不知道在哪里/如何做

My edit.html.erb:

<h1>Editing Survey</h1>

<%= render :partial => 'form' %>
<% form_for(@survey) do |f| %>
  <%= f.error_messages %>

  <p>
    <%= f.label :title %><br />
    <%= f.text_field :title %>
  </p>

  <div id="questions">
    <% f.fields_for :questions do |q| %>
      <%= render :partial => 'question', :locals => { :pf => q } %>
    <% end %>
  </div>

  <%= add_a_new_child_link("New question", f, :questions) %>
<% end %>
<div class="question">
  <%= pf.label :question %>
  <%= pf.text_field :question %>

  <% pf.fields_for :category do |c| %>
    <p>
      <%= c.label :name, "Category:" %>
      <%= c.text_field :name %>
    </p>
  <% end %>
</div>
编辑调查
'表格'%>
My_form.html.erb:

<h1>Editing Survey</h1>

<%= render :partial => 'form' %>
<% form_for(@survey) do |f| %>
  <%= f.error_messages %>

  <p>
    <%= f.label :title %><br />
    <%= f.text_field :title %>
  </p>

  <div id="questions">
    <% f.fields_for :questions do |q| %>
      <%= render :partial => 'question', :locals => { :pf => q } %>
    <% end %>
  </div>

  <%= add_a_new_child_link("New question", f, :questions) %>
<% end %>
<div class="question">
  <%= pf.label :question %>
  <%= pf.text_field :question %>

  <% pf.fields_for :category do |c| %>
    <p>
      <%= c.label :name, "Category:" %>
      <%= c.text_field :name %>
    </p>
  <% end %>
</div>



'question',:locals=>{:pf=>q}%>
My_question.html.erb:

<h1>Editing Survey</h1>

<%= render :partial => 'form' %>
<% form_for(@survey) do |f| %>
  <%= f.error_messages %>

  <p>
    <%= f.label :title %><br />
    <%= f.text_field :title %>
  </p>

  <div id="questions">
    <% f.fields_for :questions do |q| %>
      <%= render :partial => 'question', :locals => { :pf => q } %>
    <% end %>
  </div>

  <%= add_a_new_child_link("New question", f, :questions) %>
<% end %>
<div class="question">
  <%= pf.label :question %>
  <%= pf.text_field :question %>

  <% pf.fields_for :category do |c| %>
    <p>
      <%= c.label :name, "Category:" %>
      <%= c.text_field :name %>
    </p>
  <% end %>
</div>



针对您的情况的快速修复方法是使用虚拟属性。 例如,在你的问题模型中:

def category_name=(new_name)
 if category then
   category.name = new_name
 else
   category = Category.new(:name => new_name)
 end
end

def category_name
  return category.name if category 
  ""
end
在你的问题中,没有必要使用嵌套形式。只需添加如下内容:

<%= pf.text_field :category_name %>


我没有对它进行测试,但你可能发现了这个想法。

我实际上是自己解决了这个问题,而不是问。新的,在将问题传递给JS生成器函数之前,我只需要做一个Question.category.build。但我真的更喜欢你的想法,我会很快尝试一下。