我正在尝试在 posts/index.html.erb 上显示类似的内容
Post #1
Comment #1 for Post #1
Comment #2
Post #2
Comment #1 for Post #2
etc.
如果我转到 /posts/1/comments/、/posts/2/comments/ 等,它工作正常
由于它使用的是索引文件,因此 URL 中没有 :post_id 并且会引发 nil 错误。模型使用适当的have_many 和belongs_to。
这是routes.rb的一部分
resources :posts do
resources :comments
end
resources :posts
这是我的posts_controller.rb 的一部分
def index
@posts = Post.all
@comments = params[:post_id][:desc]
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @posts }
end
end
这是 index.html.erb 的一部分
<% @posts.each do |post| %>
<tr>
<td><%= post.title %></td>
<td><%= link_to 'Show', post %></td>
<td><%= link_to 'Edit', edit_post_path(post) %></td>
<td><%= link_to 'Destroy', post, :confirm => 'Are you sure?', :method => :delete %></td>
</tr>
<tr><td><%= @comments %></td></tr>
<% end %>
</table>
谢谢!