当本地人出现时,为什么我仍然会收到model_name错误?

时间:2012-07-27 02:41:59

标签: ruby-on-rails ruby-on-rails-3 ruby-on-rails-3.1

我正试图在我的PetsController.index

中的另一个视图中渲染我的狗形态
def index
  @page_title = "Add Pets"
  @title = "Add Pets"
end

但我收到错误:

NoMethodError in Pets#index

Showing C:/testapp/app/views/dogs/_form.html.erb where line #1 raised:

undefined method `model_name' for NilClass:Class

1: <%= form_for @dog do |f| %>

宠物/ index.html.erb

<div class="tab-pane" id="tab4">
   <%= render "global/flash_message" %>
   <%= render :partial => "dogs/form", :locals => {:dog => @dog } %>
</div>

我知道我能做到:

def index
  @page_title = "Add Pets"
  @title = "Add Pets"
  @dog = Dog.new
end

但当地人的意义何在?缺少什么,所以我可以用DogsController来代替它?

谢谢!

1 个答案:

答案 0 :(得分:4)

如果@dog操作中未指定#index,则您在调用nil时将dog分配给render :partial本地。您可以将实例分配保留在操作之外并在索引视图中执行(即:locals => {:dog => Dog.new},但它必须在某处发生。