Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 无法编辑或查看带有评论的帖子应用程序_Ruby On Rails_Ruby_Ruby On Rails 3 - Fatal编程技术网

Ruby on rails 无法编辑或查看带有评论的帖子应用程序

Ruby on rails 无法编辑或查看带有评论的帖子应用程序,ruby-on-rails,ruby,ruby-on-rails-3,Ruby On Rails,Ruby,Ruby On Rails 3,我已经构建了一个简单的Rails应用程序,其中包含带有一条/多条评论的帖子 我想创建一个简单的帖子视图,允许我查看帖子和相关评论。我希望每个评论都有链接-查看,编辑,删除 然而,每当我试图修改下面的代码时,我都会遇到路由错误。帮忙 routes.rb resources :posts do resources :comments end 耙道 post_comments GET /posts/:post_id/comments(.:format) comment

我已经构建了一个简单的Rails应用程序,其中包含带有一条/多条评论的帖子

我想创建一个简单的帖子视图,允许我查看帖子和相关评论。我希望每个评论都有链接-查看,编辑,删除

然而,每当我试图修改下面的代码时,我都会遇到路由错误。帮忙

routes.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
注释\u controller.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
comments\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>

评论人:

评论:

| | “你确定吗?”, :方法=>:删除%>

您是否看到:

路由错误 没有路由匹配{:action=>“show”,:controller=>“comments”} 尝试运行rake路由以获取有关可用路由的更多信息

我用您提供的代码复制了您的项目,只收到了路由错误,因为没有将id传递给路由助手方法。因为这些是restful路由,所以视图注释的格式应该是/comments/:id(:format)

我可以通过将id或comment对象传递到comment\u路径并编辑\u comment\u路径帮助器方法来解决此错误,如下所示:

<%= link_to 'View Comment', comment_path(2) %> |
<%= link_to 'Edit Comment', edit_comment_path(3) %> |
<%= link_to 'Delete Comment', [@post, comment],
    :confirm => 'Are you sure?',
    :method => :delete %></p>
|
|
“你确定吗?”,
:方法=>:删除%>

显然,您希望使用正确的id或注释对象来填充它们,而不仅仅是一些随机id

希望这有帮助


干杯

如何返回正确的记录,而不是随机数/记录?您正在comments控制器的show操作中为实例变量@comment分配当前注释。因此,当您在show视图上构建这些链接时,请输入@comment.id
| |你确定吗?,:method=>:delete%>

,而不是将随机id传递给comment\u path和edit\u comment\u path,除非我完全误解了问题:D