Private Method to Load Parent Resource Failing

2019-09-09 09:38发布

From Previous:

Rails 4 Nested Resources/Routes... almost there...?

My private method within my lines controller to load the manufacturer into the controller is throwing an error...

I use before_filter :load_manufacturer

and the function is:

def load_manufacturer
  @manufacturer = Manufacturer.find(params[:manufacturer_id])
end

When I try to edit the line instance in the form, I get:

Couldn't find Manufacturer with id=1

But I'm trying to edit a child resource (1) of parent resource (37)...

http://localhost:3000/manufacturers/37/lines/1

The parameters being passed here are:

{"manufacturer_id"=>"manufacturer_id","id"=>"1"}

And my form, which MAKES these models just fine, is:

app/views/lines/_form.html.erb

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

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

  <div class="field">
    <%= f.label :name %><br>
    <%= f.text_field :name %>
  </div>
  <div class="field">
    <%= f.label :manufacturer_id %><br>
    <%= f.text_field :manufacturer_id %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

0条回答
登录 后发表回答