Ruby on rails 删除注释显示路由错误没有路由匹配{:action=>;show";,:controller=>;articles";,:id=>;nil}
我一直坚持删除一篇文章的评论。我可以显示和编辑评论,但不能删除它们。我尝试了各种组合,并在网上搜索,但可以找到合适的答案。我在comments/_comment.html.erb部分中添加了“Delete”链接 comments/_comment.html.ervRuby on rails 删除注释显示路由错误没有路由匹配{:action=>;show";,:controller=>;articles";,:id=>;nil},ruby-on-rails,ruby,hyperlink,routing,Ruby On Rails,Ruby,Hyperlink,Routing,我一直坚持删除一篇文章的评论。我可以显示和编辑评论,但不能删除它们。我尝试了各种组合,并在网上搜索,但可以找到合适的答案。我在comments/_comment.html.erb部分中添加了“Delete”链接 comments/_comment.html.erv <% if @article.comments.count >= 1 %> <div style="border: px solid #66c9ee;border-radius: 0px 0px 0px
<% 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>
<li> <div style="color: #0077CC; background-color:; margin-top:4px; margin-left:25px;"> <%= link_to 'Delete', article_comment_path(@article,comment), :confirm => 'Are you sure?', :method => :delete %> </div></li>
</ul>
</div>
<% else %>
<div style="color:#0077CC;margin-left:25px;font-size:1.4em;"> be first to comment</div>
<% end %>
articles\u controller.rb
class CommentsController < ApplicationController
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])
@article = @comment.article
# @article = Article.find(params[:article_id])
#@comment = @article.comments
end
def update
@comment = Comment.find(params[:id])
@article = @comment.article
if @comment.update_attributes(params[:comment])
flash[:success] = "Comment updated!"
redirect_to article_path(@article)
else
render :action => "edit"
end
end
def destroy
@article = Article.find(params[:article_id])
@comment = @article.comments.find(params[:id])
@comment.destroy
redirect_to article_path(@artilce)
end
end
class ArticlesController < ApplicationController
before_filter :is_user_admin, only: [:new, :create, :edit, :destroy]
before_filter :log_impression, :only=> [:show]
def is_user_admin
redirect_to(action: :index) unless current_user.try(:is_admin?)
return false
end
def log_impression
@article = Article.find(params[:id])
# this assumes you have a current_user method in your authentication system
@article.impressions.create(ip_address: request.remote_ip,user_id:current_user.id)
end
def index
@articles = Article.all(:order => "created_at DESC")
@article_titles = Article.first(10)
@tags = Tag.all
end
def show
@article = Article.find(params[:id])
@related_articles = Article.joins(:taggings).where('articles.id != ?', @article.id).where(taggings: { tag_id: @article.tag_ids })
@article_popular = Article.order('articles.impressions_count DESC').limit(5)
end
def new
@article = Article.new
end
def create
@article = Article.new(params[:article])
@article.user_id = current_user.id
if @article.save
flash[:success] = "article created!"
redirect_to article_path(@article)
else
render 'new'
end
end
def destroy
@article = Article.find(params[:id])
@article.destroy
redirect_to action: 'index'
end
def edit
@article = Article.find(params[:id])
end
def update
@article = Article.find(params[:id])
if @article.update_attributes(params[:article])
flash.notice = "Article '#{@article.title}' Updated!"
redirect_to article_path(@article)
else
render 'edit'
end
end
end
<div class="row-fluid">
<div class="span12">
<div class ="span9">
<div class ="row-fluid">
<div id="upperborder">
<div id="lowerborder">
<div style="color:#1fb2e8;font-size:2.3em;"><%= @article.title %></div>
<div id="tabs">
<ul id="tabs">
<li> <%= time_tag(@article.created_at.in_time_zone("Asia/Calcutta")) %> </li>
<li> <%= @article.user.username if @article.user %></li>
<li>
<% unless @article.comments.empty? %>
(<%= @article.comments.size %>) comments
<% end %>
</li>
<li>
<% @article.tags.each do |tag| %>
<%= link_to tag.name, tag_path(tag) %>
<% end %>
</li>
</ul>
</div>
<hr id="upperline">
<div style="font-size: 1.2em"><%= @article.body %></div>
<div id="tabs">
<ul id="tabs">
<div style="margin-top:20px; margin-left:10px"> <li> <%= link_to "back",articles_path, :class => '' %></li></div>
<% if current_user.try (:is_admin) %>
<div style="margin-top:20px;margin-left:-10px""> <li> <%= link_to "edit", edit_article_path(@article), :class => '' %></li> </div>
<div style="margin-top:20px"> <li> <%= link_to "delete",article_path(@article),:method => :delete,:confirm => 'Are you sure?',:class => '' %></li> </div>
<% end %>
</ul>
</div>
<div id="fabs">
<ul id="fabula">
<div style="margin-left:250px; font-size:1.2em;color:#1fb2e8;margin-top:-0px"> Viewed: <%=@article.impression_count %> times,
<% if (@article.created_at != @article.updated_at) %>
edit: <%= time_ago_in_words(@article.updated_at) %> ago,
<% end %>
<% if ! @article.comments.empty? %>
Active: <%= time_ago_in_words(@article.comments.last.created_at) %> ago
<% end %>
</div>
</ul>
</div>
<hr style="border: 1px solid #E0E0F0; margin-top:50px">
<div id="commentform">
<%= render :partial => 'comments/comment_form' %>
<% @article.comments.each do |c| %>
<% if !c.nil? %>
<div id ="commentdisplay"> <%= render partial: 'comments/comment', :locals => { :comment => c } %> </div>
<% end %>
<% end %>
</div>
</div>
</div>
</div>
<div class="span3">
<div id="popular">
<% if !@article_popular.empty? %>
<div id="headerpopular"> most viewed articles </div>
<% @article_popular.each do |article_popular| %>
<div id="popular-title"> <%= link_to truncate(article_popular.title, :length => 35, :separator => ' '), article_path(article_popular) %></div>
<% end %>
<% end %>
</div>
</div>
</div>
<% if !@related_articles.empty? %>
<div id = "related-border">
<div id="related-title"> Related articles </div>
<% @related_articles.each do | related_article | %>
<div id="related-title-link"> <%= link_to truncate(related_article.title, :length => 35, :separator => ' '), article_path(related_article) %></div>
<% end %>
</div>
<% else %>
<div id="related-border">
<div id="ygm"> You got us ! </div>
<div id="nofound"><h3> no related article found :(</h3></div>
</div>
<% end %>
class-ArticlesController[:show]
def是用户管理员
除非是当前用户,否则重定向到(操作::索引)。try(:is_admin?)
返回错误
结束
def log_印模
@article=article.find(参数[:id])
#这假设您在身份验证系统中有当前的用户方法
@article.impressions.create(ip地址:request.remote\u ip,用户id:current\u user.id)
结束
def索引
@articles=Article.all(:order=>“在DESC创建”)
@文章标题=第一篇文章(10)
@tags=Tag.all
结束
def秀
@article=article.find(参数[:id])
@related_articles=Article.joins(:taggings)。其中('articles.id!=?',@Article.id)。其中(taggings:{tag_id:@Article.tag_id})
@article_popular=article.order('articles.impressions_count DESC')。限制(5)
结束
def新
@article=article.new
结束
def创建
@article=article.new(参数[:article])
@article.user\u id=当前的\u user.id
如果@article.save
flash[:success]=“文章已创建!”
重定向到文章路径(@article)
其他的
呈现“新”
结束
结束
def销毁
@article=article.find(参数[:id])
@第条销毁
将_重定向到操作:“索引”
结束
定义编辑
@article=article.find(参数[:id])
结束
def更新
@article=article.find(参数[:id])
如果@article.update_属性(参数[:article])
flash.notice=“文章“#{@Article.title}”已更新!”
重定向到文章路径(@article)
其他的
渲染“编辑”
结束
结束
结束
articles/show.html.erb
class CommentsController < ApplicationController
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])
@article = @comment.article
# @article = Article.find(params[:article_id])
#@comment = @article.comments
end
def update
@comment = Comment.find(params[:id])
@article = @comment.article
if @comment.update_attributes(params[:comment])
flash[:success] = "Comment updated!"
redirect_to article_path(@article)
else
render :action => "edit"
end
end
def destroy
@article = Article.find(params[:article_id])
@comment = @article.comments.find(params[:id])
@comment.destroy
redirect_to article_path(@artilce)
end
end
class ArticlesController < ApplicationController
before_filter :is_user_admin, only: [:new, :create, :edit, :destroy]
before_filter :log_impression, :only=> [:show]
def is_user_admin
redirect_to(action: :index) unless current_user.try(:is_admin?)
return false
end
def log_impression
@article = Article.find(params[:id])
# this assumes you have a current_user method in your authentication system
@article.impressions.create(ip_address: request.remote_ip,user_id:current_user.id)
end
def index
@articles = Article.all(:order => "created_at DESC")
@article_titles = Article.first(10)
@tags = Tag.all
end
def show
@article = Article.find(params[:id])
@related_articles = Article.joins(:taggings).where('articles.id != ?', @article.id).where(taggings: { tag_id: @article.tag_ids })
@article_popular = Article.order('articles.impressions_count DESC').limit(5)
end
def new
@article = Article.new
end
def create
@article = Article.new(params[:article])
@article.user_id = current_user.id
if @article.save
flash[:success] = "article created!"
redirect_to article_path(@article)
else
render 'new'
end
end
def destroy
@article = Article.find(params[:id])
@article.destroy
redirect_to action: 'index'
end
def edit
@article = Article.find(params[:id])
end
def update
@article = Article.find(params[:id])
if @article.update_attributes(params[:article])
flash.notice = "Article '#{@article.title}' Updated!"
redirect_to article_path(@article)
else
render 'edit'
end
end
end
<div class="row-fluid">
<div class="span12">
<div class ="span9">
<div class ="row-fluid">
<div id="upperborder">
<div id="lowerborder">
<div style="color:#1fb2e8;font-size:2.3em;"><%= @article.title %></div>
<div id="tabs">
<ul id="tabs">
<li> <%= time_tag(@article.created_at.in_time_zone("Asia/Calcutta")) %> </li>
<li> <%= @article.user.username if @article.user %></li>
<li>
<% unless @article.comments.empty? %>
(<%= @article.comments.size %>) comments
<% end %>
</li>
<li>
<% @article.tags.each do |tag| %>
<%= link_to tag.name, tag_path(tag) %>
<% end %>
</li>
</ul>
</div>
<hr id="upperline">
<div style="font-size: 1.2em"><%= @article.body %></div>
<div id="tabs">
<ul id="tabs">
<div style="margin-top:20px; margin-left:10px"> <li> <%= link_to "back",articles_path, :class => '' %></li></div>
<% if current_user.try (:is_admin) %>
<div style="margin-top:20px;margin-left:-10px""> <li> <%= link_to "edit", edit_article_path(@article), :class => '' %></li> </div>
<div style="margin-top:20px"> <li> <%= link_to "delete",article_path(@article),:method => :delete,:confirm => 'Are you sure?',:class => '' %></li> </div>
<% end %>
</ul>
</div>
<div id="fabs">
<ul id="fabula">
<div style="margin-left:250px; font-size:1.2em;color:#1fb2e8;margin-top:-0px"> Viewed: <%=@article.impression_count %> times,
<% if (@article.created_at != @article.updated_at) %>
edit: <%= time_ago_in_words(@article.updated_at) %> ago,
<% end %>
<% if ! @article.comments.empty? %>
Active: <%= time_ago_in_words(@article.comments.last.created_at) %> ago
<% end %>
</div>
</ul>
</div>
<hr style="border: 1px solid #E0E0F0; margin-top:50px">
<div id="commentform">
<%= render :partial => 'comments/comment_form' %>
<% @article.comments.each do |c| %>
<% if !c.nil? %>
<div id ="commentdisplay"> <%= render partial: 'comments/comment', :locals => { :comment => c } %> </div>
<% end %>
<% end %>
</div>
</div>
</div>
</div>
<div class="span3">
<div id="popular">
<% if !@article_popular.empty? %>
<div id="headerpopular"> most viewed articles </div>
<% @article_popular.each do |article_popular| %>
<div id="popular-title"> <%= link_to truncate(article_popular.title, :length => 35, :separator => ' '), article_path(article_popular) %></div>
<% end %>
<% end %>
</div>
</div>
</div>
<% if !@related_articles.empty? %>
<div id = "related-border">
<div id="related-title"> Related articles </div>
<% @related_articles.each do | related_article | %>
<div id="related-title-link"> <%= link_to truncate(related_article.title, :length => 35, :separator => ' '), article_path(related_article) %></div>
<% end %>
</div>
<% else %>
<div id="related-border">
<div id="ygm"> You got us ! </div>
<div id="nofound"><h3> no related article found :(</h3></div>
</div>
<% end %>
-
()评论
-
- ''%>
您的
评论控制器#destroy
操作中有一个输入错误:
redirect_to article_path( @artilce )
应该是
redirect_to article_path( @article )
(注意@artilce
)
因此,我猜您的销毁操作是有效的,但重定向不起作用,因为@artilce
返回nil
(因此出现了奇怪的错误消息) 变化
<%= link_to 'Delete', article_comment_path(@article,comment), :confirm => 'Are you sure?', :method => :delete %>
“你确定吗?”,:method=>:delete%>
到
“你确定吗?”,:method=>:delete%>
您可以在以下网站获得更多信息:hi@m\x,谢谢是的,您是对的。我犯了一个愚蠢的错误。