我已经构建了一个简单的 Rails 应用程序,其中包含一个/多个评论的帖子。
我想创建一个简单的帖子视图,让我可以查看帖子和相关评论。我希望每条评论都有链接——查看、编辑、删除。
但是,每当我尝试修改下面的代码时,都会出现路由错误。帮助?
路线.rb
resources :posts do
resources :comments
end
耙路线
post_comments GET /posts/:post_id/comments(.:format) comments#index
POST /posts/:post_id/comments(.:format) comments#create
new_post_comment GET /posts/:post_id/comments/new(.:format) comments#new
edit_post_comment GET /posts/:post_id/comments/:id/edit(.:format) comments#edit
post_comment GET /posts/:post_id/comments/:id(.:format) comments#show
PUT /posts/:post_id/comments/:id(.:format) comments#update
DELETE /posts/:post_id/comments/:id(.:format) comments#destroy
评论控制器.rb
def show
@comment = Comment.find(params[:id])
respond_to do |format|
format.html
format.json { render :json => @post }
end
end
def edit
@comment = Comment.find(params[:id])
end
评论\show.html.erb
<p>
<b>Commenter:</b>
<%= @comment.user_id %>
</p>
<p>
<b>Comment:</b>
<%= @comment.text %>
</p>
<%= link_to 'View Comment', comment_path(?) %> |
<%= link_to 'Edit Comment', edit_comment_path(?) %> |
<%= link_to 'Delete Comment', [@post, comment],
:confirm => 'Are you sure?',
:method => :delete %></p>