我到此爲止:香港專業教育學院創建了一個新的Rails應用程序: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" %>
然後我複製了代碼的形式後/指數發佈/ _index.html.erb
但不知何故,在歡迎的/ home我的部分不會工作,我的錯誤:
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>
那我錯了什麼?
'@posts = nil',你的控制器已經'@posts =帖子.all'? – gmaliar
有沒有另一種方法,所以我不必在我的控制器中定義它新的,只是可以重用後控制器? –
看看https://github.com/josevalim/inherited_resources – gmaliar