ACI Engineer
ACI Engineer

Reputation: 87

One of my nested resources is not being created

First off, let me thank you if you're taking the time to look at this. Thanks :).

So, I finally figured out how to properly nest resources in the top resource's _form.html.erb. My final project will have the following nested resources from top to bottom: IDFs => Switches => Jacks. Currently I have everything working up through creating a new jack...which gives me the following error:

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>

All of my models were scaffolded, and I haven't touched many of their files, except for the models to associate, the controllers to fix routes, and the _form.html.erb files. I'll post the code I believe to be relevant, but if there is anything else that would help you help me if you saw it then let me know.

app/controllers/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
...

app/views/switches/_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 %>

I've already tried to implement some code from a couple of similar questions on Stack Overflow, but nothing seems to work. Any help would be much appreciated. Also, if there is any other code you want to see, just let me know.

Thanks again!

Upvotes: 0

Views: 89

Answers (1)

Bryan
Bryan

Reputation: 3280

Have you try building the @jack object from your parent resource? i.e.

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

Upvotes: 1

Related Questions