Ruby on rails 没有路由匹配{:action=>;edit";,:controller=>;comments";,:article\u id=>;nil,:id=>;nil}来编辑ruby on rails博客中文章的评论
在我决定添加评论编辑功能之前,我的应用程序一直运行良好。在此之前,我可以查看特定的文章,但现在不行。现在,当我尝试访问带有一些注释的特定文章(articles/show)时,我得到了路由错误,因为Ruby on rails 没有路由匹配{:action=>;edit";,:controller=>;comments";,:article\u id=>;nil,:id=>;nil}来编辑ruby on rails博客中文章的评论,ruby-on-rails,ruby,routes,Ruby On Rails,Ruby,Routes,在我决定添加评论编辑功能之前,我的应用程序一直运行良好。在此之前,我可以查看特定的文章,但现在不行。现在,当我尝试访问带有一些注释的特定文章(articles/show)时,我得到了路由错误,因为没有路由匹配{:action=>“edit”,:controller=>“comments”,:article_id=>nil,:id=>nil}。 实际上,我正在尝试为文章实现评论编辑功能。为此,我在comments/_comment.html.erb中为注释添加了“编辑”链接。值得注意的是,只有当文
没有路由匹配{:action=>“edit”,:controller=>“comments”,:article_id=>nil,:id=>nil}
。
实际上,我正在尝试为文章实现评论编辑功能。为此,我在comments/_comment.html.erb中为注释添加了“编辑”链接。值得注意的是,只有当文章有注释时,我才会收到此错误,而当文章没有注释时,我不会收到此错误。
我对文章和评论有不同的看法
注释\u controller.rb
class CommentsController < ApplicationController
before_filter :user_signed_in, except: [:create]
def new
@comment = Comment.new
end
def create
@article = Article.find(params[:article_id])
@comment = @article.comments.build(params[:comment])
@comment.user_id = current_user.id
@comment.save
flash[:success] = "Comment created!"
redirect_to article_path(@comment.article)
end
def edit
@comment = Comment.find(params[:id])
end
def update
@comment = Comment.find(params[:id])
@article = @comment.article
respond_to do |format|
if @comment.update_attributes(params[:comment])
redirect_to @article_path(@article)
else
render :action => "edit"
end
end
def destroy
@comment = Comment.find(params[:id])
@article = Article.find(params[:article_id])
@comment.destroy
redirect_to @article_path(@artilce)
end
end
routes.rb
Mau::Application.routes.draw do
devise_for :users
root :to => 'articles#index'
resources :articles
resources :dashboard
resources :tags
resources :articles do
resources :comments
end
end
comments\u comment.html.erb
<% if @article.comments.count >= 1 %>
<div style="border: px solid #66c9ee;border-radius: 0px 0px 0px 0px;margin: 10px -30px 15px; padding: 10px 15px 25px; background: none repeat scroll 0 0 #F2F2F2; width:700px; font-size: 1.2em;border-bottom: 0px solid #DDDDDD;">
<%= comment.content %>
<div id="tabula">
<ul id="tabula">
<li> <div style="color: #0077CC;margin-rigth:200px; font-size: 1.0em;margin-top:4px;background-color:#;"> <%= comment.user.username if comment.user %></div></li>
<li> <div style="color: #0077CC; background-color:; margin-top:4px; margin-left:25px;"> <p> <%= time_ago_in_words(comment.created_at.in_time_zone("Asia/Calcutta")) unless comment.created_at.nil? %> </p></div></li>
<li> <div style="color: #0077CC; background-color:; margin-top:4px; margin-left:25px;"> <%= link_to "edit", edit_article_comment_path(@article ,@comment) %> </div></li>
</ul>
</div>
</div>
<% else %>
<div style="color:#0077CC;margin-left:25px;font-size:1.4em;"> be first to comment</div>
<% end %>
=1%>
首先发表评论
另外,请注意在comment/\u comment.html.erb中的
编辑文章注释路径中使用注释
而不是@comment
。仅当方法计数返回1或更大时才显示注释。当您调用count方法时,它会直接在数据库中计数,对于没有任何注释的文章,它不会呈现任何内容,这是因为您可以看到没有注释的文章
但是,当注释存在时,您将构建一个新注释以显示在表单中,并尝试为其创建一个编辑链接
因此,我的解决方案是,更改您的表单,因为您不需要再次重新发送所有注释,所以将新注释作为参数传递,而不是在注释集合上构建一个注释。然后在另一个视图上,您可以为每个保留的评论创建一个编辑链接。您可以发布您的文章视图和您的“评论部分”吗?嗨,alexBrand,我已经添加了“评论部分”。关于文章视图,您是否询问文章/显示或索引页面。我已经添加了文章/显示页面。如果您需要更多的代码来粘贴,请让我知道。可以显示您的rake routes结果吗?可能有错误的路径Pathi Nich,我已经添加了rake路径结果。请检查问题。嗨,当我访问一篇没有评论的文章时,我没有发现这个错误。我在comments/_comment.html.erb中添加了“编辑”链接。请看一下。就连我也感觉到问题在于评论的局部性。但我确实不知道。我把“编辑”链接放在了comments/_comment.html.erb上。您好,Sunxperous,但是当编辑操作在comments_controller.rb中时,它如何显示路由错误。我已经扩展了我的答案,请查看它是否解决了路由错误问题。该错误可能是由edit\u article\u comment\u path
引起的。嗨,Sunxperous,我为我工作过。现在我们可以显示一篇文章的所有评论,点击编辑链接显示编辑表单,但问题是,当我点击编辑链接时,它并没有显示其中的内容。我的意思是它向我们显示了空文本区域。但出于编辑目的,正如我们在stakoverflow上看到的,当我们编辑问题时,文本框会显示其中的原始内容。任何建议,我认为问题出在comments_controller.rb(带有编辑和更新操作)中。将([@article,@article.comments.build])
的form_替换为@comment
,因为@comment
是在编辑操作中检索到的注释。不需要包括隐藏的\u字段:article\u id
,因为注释所属的文章在更新操作中通过@article=@comment.article
公开。不过,我可能对文章部分有错误。嗨,保罗,谢谢你的建议。我需要更改表单|中的参数还是需要更改表单|本身。您能解释一下吗。谢谢您必须更改表单|中的行以获得此“true do | f |%>”您好Paulo Henrique,我尝试了您的建议,但仍然有错误。没有路由匹配{:action=>“edit”,:controller=>“comments”,:article_id=>#,:id=>nil}。它显示id为零。这是在制造问题。
<% if @article.comments.count >= 1 %>
<div style="border: px solid #66c9ee;border-radius: 0px 0px 0px 0px;margin: 10px -30px 15px; padding: 10px 15px 25px; background: none repeat scroll 0 0 #F2F2F2; width:700px; font-size: 1.2em;border-bottom: 0px solid #DDDDDD;">
<%= comment.content %>
<div id="tabula">
<ul id="tabula">
<li> <div style="color: #0077CC;margin-rigth:200px; font-size: 1.0em;margin-top:4px;background-color:#;"> <%= comment.user.username if comment.user %></div></li>
<li> <div style="color: #0077CC; background-color:; margin-top:4px; margin-left:25px;"> <p> <%= time_ago_in_words(comment.created_at.in_time_zone("Asia/Calcutta")) unless comment.created_at.nil? %> </p></div></li>
<li> <div style="color: #0077CC; background-color:; margin-top:4px; margin-left:25px;"> <%= link_to "edit", edit_article_comment_path(@article,comment) %> </div></li>
</ul>
</div>
</div>
<% else %>
<div style="color:#0077CC;margin-left:25px;font-size:1.4em;"> be first to comment</div>
<% end %>
articles GET /articles(.:format) articles#index
POST /articles(.:format) articles#create
new_article GET /articles/new(.:format) articles#new
edit_article GET /articles/:id/edit(.:format) articles#edit
article GET /articles/:id(.:format) articles#show
PUT /articles/:id(.:format) articles#update
DELETE /articles/:id(.:format) articles#destroy
dashboard_index GET /dashboard(.:format) dashboard#index
POST /dashboard(.:format) dashboard#create
new_dashboard GET /dashboard/new(.:format) dashboard#new
edit_dashboard GET /dashboard/:id/edit(.:format) dashboard#edit
dashboard GET /dashboard/:id(.:format) dashboard#show
PUT /dashboard/:id(.:format) dashboard#update
DELETE /dashboard/:id(.:format) dashboard#destroy
tags GET /tags(.:format) tags#index
POST /tags(.:format) tags#create
new_tag GET /tags/new(.:format) tags#new
edit_tag GET /tags/:id/edit(.:format) tags#edit
tag GET /tags/:id(.:format) tags#show
PUT /tags/:id(.:format) tags#update
DELETE /tags/:id(.:format) tags#destroy
article_comments GET /articles/:article_id/comments(.:format) comments#index
POST /articles/:article_id/comments(.:format) comments#create
new_article_comment GET /articles/:article_id/comments/new(.:format) comments#new
edit_article_comment GET /articles/:article_id/comments/:id/edit(.:format) comments#edit
article_comment GET /articles/:article_id/comments/:id(.:format) comments#show
PUT /articles/:article_id/comments/:id(.:format) comments#update
DELETE /articles/:article_id/comments/:id(.:format) comments#destroy
GET /articles(.:format) articles#index
POST /articles(.:format) articles#create
GET /articles/new(.:format) articles#new
GET /articles/:id/edit(.:format) articles#edit
GET /articles/:id(.:format) articles#show
PUT /articles/:id(.:format) articles#update
DELETE /articles/:id(.:format) articles#destroy
Mau::Application.routes.draw do
devise_for :users
root :to => 'articles#index'
resources :articles
resources :dashboard
resources :tags
resources :articles do
resources :comments
end
end
<% if @article.comments.count >= 1 %>
<div style="border: px solid #66c9ee;border-radius: 0px 0px 0px 0px;margin: 10px -30px 15px; padding: 10px 15px 25px; background: none repeat scroll 0 0 #F2F2F2; width:700px; font-size: 1.2em;border-bottom: 0px solid #DDDDDD;">
<%= comment.content %>
<div id="tabula">
<ul id="tabula">
<li> <div style="color: #0077CC;margin-rigth:200px; font-size: 1.0em;margin-top:4px;background-color:#;"> <%= comment.user.username if comment.user %></div></li>
<li> <div style="color: #0077CC; background-color:; margin-top:4px; margin-left:25px;"> <p> <%= time_ago_in_words(comment.created_at.in_time_zone("Asia/Calcutta")) unless comment.created_at.nil? %> </p></div></li>
<li> <div style="color: #0077CC; background-color:; margin-top:4px; margin-left:25px;"> <%= link_to "edit", edit_article_comment_path(@article ,@comment) %> </div></li>
</ul>
</div>
</div>
<% else %>
<div style="color:#0077CC;margin-left:25px;font-size:1.4em;"> be first to comment</div>
<% end %>
<% if @article.comments.count >= 1 %>
<div "...">
<%= comment.content %> # <-- This.
<% if @article.comments.count >= 1 %>
<div "...">
<% @article.comments.each do |comment| %> # <-- This.
<%= comment.content %>
...
<% end %> # Remember to close the loop.
</div>
<% else %>
...