到目前为止我的步骤:我创建了一个新的 rails 应用程序:
rails new sample
cd sample
rails g scaffold Post name:string content:string
rails g controller welcome home
下一步我在欢迎/主页视图中添加了一行:
<h1>Welcome#home</h1>
<p>Find me in app/views/welcome/home.html.erb</p>
<%= render "posts/index" %>
然后我将代码表单 post/index 复制到 post/_index.html.erb
但不知何故,我的部分在欢迎/家庭中不起作用,我的错误:
NoMethodError in Welcome#home
Showing C:/Sites/sample/app/views/posts/_index.html.erb where line #12 raised:
undefined method `each' for nil:NilClass
Extracted source (around line #12):
9: <th></th>
10: </tr>
11:
12: <% @posts.each do |post| %>
13: <tr>
14: <td><%= post.name %></td>
15: <td><%= post.content %></td>
所以我做错了什么?