0

首先,如果你花时间看这个,让我谢谢你。谢谢 :)。

所以,我终于想出了如何在顶级资源的_form.html.erb中正确嵌套资源。我的最终项目将有以下从上到下的嵌套资源:IDFs => Switches => Jacks。目前,我通过创建一个新的插孔来完成所有工作......这给了我以下错误:

NoMethodError in Jacks#new

Showing /var/rails/network/app/views/jacks/_form.html.erb where line #1 raised:

undefined method `model_name' for NilClass:Class
Extracted source (around line #1):

1: <%= form_for [@switch, @jack] do |f| %>
2:   <% if @jack.errors.any? %>
3:     <div id="error_explanation">
4:       <h2><%= pluralize(@jack.errors.count, "error") %> prohibited this jack from being saved:</h2>

我所有的模型都是脚手架的,除了要关联的模型、修复路由的控制器和 _form.html.erb 文件之外,我还没有接触过它们的许多文件。我会发布我认为相关的代码,但如果你看到它还有什么可以帮助你的,请告诉我。

应用程序/控制器/jacks_controller.rb:

...
  def new
    @jacks = Jack.new(:switch_id => params[:switch_id])

    respond_to do |format|
      format.html # new.html.erb
      format.json { render :json => @jack }
    end
  end
...

应用程序/视图/开关/_form.html.erb:

<%= form_for [@idf, @switch] do |switch_form| %>
    <% if @switch.errors.any? %>
      <div id="error_explanation">
        <h2><%= pluralize(@switch.errors.count, "error") %> prohibited this switch from being saved:</h2>

        <ul>
        <% @switch.errors.full_messages.each do |msg| %>
          <li><%= msg %></li>
        <% end %>
        </ul>
      </div>
    <% end %>

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

    <% switch_form.fields_for :jacks do |jack_form| %>
      <p>
        <div class="field">
          <%= jack_form.label :port, 'Port:' %>
          <%= jack_form.text_field :port %>
        </div>
        <div class="field">
          <%= jack_form.label :jack_number, 'Jack Number:' %>
          <%= jack_form.text_field :jack_number %>
        </div>
        <div class="field">
          <%= jack_from.label :room_number, 'Room Number:' %>
          <%= jack_form.text_field :room_number %>
        </div>
        <% unless jack_form.object.new_record? %>
          <div>
            <%= jack_form.label :_delete, 'Remove:' %>
            <%= jack_form.check_box :_delete %>
          </div>
        <% end %>
      <p>
    <% end %>

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

app/views/jacks/_form.html.erb:

<%= form_for [@jack.switch, @jack] do |f| %>
  <% if @jack.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@jack.errors.count, "error") %> prohibited this jack from being saved:</h2>

      <ul>
      <% @jack.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :port %><br />
    <%= f.number_field :port %>
  </div>
  <div class="field">
    <%= f.label :jack_number %><br />
    <%= f.text_field :jack_number %>
  </div>
  <div class="field">
    <%= f.label :room_number %><br />
    <%= f.text_field :room_number %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

我已经尝试从 Stack Overflow 上的几个类似问题中实现一些代码,但似乎没有任何效果。任何帮助将非常感激。另外,如果您想查看任何其他代码,请告诉我。

再次感谢!

4

1 回答 1

1

您是否尝试过从您的父资源构建 @jack 对象?IE

def new
  # find Jack's parent
  parent = JacksParent.find(:parent_id)
  @jacks = parent.jacks.build(:switch_id => params[:switch_id])

  respond_to do |format|
    format.html # new.html.erb
    format.json { render :json => @jack }
  end
end
于 2012-06-27T18:34:24.040 回答