我正试图在我的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
来代替它?
谢谢!
答案 0 :(得分:4)
如果@dog
操作中未指定#index
,则您在调用nil
时将dog
分配给render :partial
本地。您可以将实例分配保留在操作之外并在索引视图中执行(即:locals => {:dog => Dog.new}
,但它必须在某处发生。