加载父资源失败的私有方法

时间:2014-03-17 19:06:56

标签: ruby-on-rails ruby ruby-on-rails-4

来自上一篇:

Rails 4嵌套资源/路由......差不多......?

我的行控制器中的私有方法将制造商加载到控制器中会引发错误...

我使用before_filter:load_manufacturer

,功能是:

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

当我尝试编辑表单中的行实例时,我得到:

Couldn't find Manufacturer with id=1

但我正在尝试编辑父资源(37)的子资源(1)......

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

这里传递的参数是:

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

我的形式,使这些模型很好,是:

应用程序/视图/线/ _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 个答案:

没有答案