如何删除 rails 博客应用程序中的评论?
How can I delete comments in a rails blog app?
我是 rails 的新手,我已经按照 Jumpstart Labs 的 Blogger 教程制作了一个博客应用程序,我正在尝试实现一项允许登录用户删除评论的功能文章上。
但我一直对这个错误消息感到困惑“ActiveRecord::RecordNotFound in CommentsController#destroy
找不到包含 'id'="
的文章
这是我到目前为止所管理的内容,
这是我的 app/controllers/comments_controller.rb
class CommentsController < ApplicationController
before_filter :require_login, except: [:create]
def create
@comment = Comment.new(comment_params)
@comment.article_id = params[:article_id]
@comment.save
redirect_to article_path(@comment.article)
end
def destroy
@article = Article.find(params[:article_id])
@comment = @article.comments.find(params[:id])
@comment.destroy
redirect_to article_path(@article)
end
private
def comment_params
params.require(:comment).permit(:author_name, :body)
end
end
还有我的/views/articles/show.html.erb
<h1><%= @article.title %></h1>
<p>
<b>Posted on <%= @article.created_at.strftime("%B %d %Y") %></b>
</p>
<p>
<% if @article.image.exists? %>
<%= image_tag @article.image.url%>
<% end %>
</p>
<p><%= @article.body %></p>
<%= render partial: 'comments/form' %>
<% if logged_in? %>
<%= link_to "Edit", edit_article_path(@article) %>
<%= link_to "delete", article_path(@article), method: :delete%>
<% end %>
<%= link_to "<< Back to Articles List", articles_path %>
<h3>Comments (<%=@article.comments.size %>) </h3>
<%= render partial: 'articles/comment',
collection: @article.comments %>
<p>
Tags:
<% @article.tags.each do |tag| %>
<%= link_to tag.name, tag_path(tag) %>
<% end %>
</p>
app/views/articles/comment.html.erb
<div>
<p><b>Comment by:</b> <%= comment.author_name %></p>
<p class="comment"><%= comment.body %></p>
<p>Posted <%= distance_of_time_in_words(comment.article.created_at, comment.created_at) %> later</p>
<%= link_to 'Delete Comment', article_comment(article, comment), method: :delete, data: { confirm: 'Are you sure?' } %>
</div>
routes.rb
Blogger::Application.routes.draw do
get 'about', to: 'info#about'
get 'portfolio', to: 'info#portfolio'
get 'contact', to: 'info#contact'
root to: 'articles#index'
resources :articles do
resources :comments
end
resources :tags
resources :authors
resources :author_sessions, only: [ :new, :create, :destroy ]
get 'login' => 'author_sessions#new'
get 'logout' => 'author_sessions#destroy'
end
和我的 articles_controller
class ArticlesController < ApplicationController
include ArticlesHelper
before_filter :require_login, only: [:new, :create, :edit, :update, :destroy]
def index
@articles = Article.all
end
def new
@article = Article.new
end
def create
@article = Article.new(article_params)
@article.save
flash.notice = "Article '#{@article.title}' Created!"
redirect_to article_path(@article)
end
def show
@article = Article.find(params[:id])
@comment = Comment.new
@comment.article_id = @article.id
end
def edit
@article = Article.find(params[:id])
end
def update
@article = Article.find(params[:id])
@article.update(article_params)
flash.notice = "Article '#{@article.title}' Updated!"
redirect_to article_path(@article)
end
def destroy
@article = Article.find(params[:id])
@article.destroy
flash.notice = "Article '#{@article.title}' Deleted!"
redirect_to articles_path
end
结束
非常感谢任何意见或建议,我一直在四处寻找 google 和堆栈溢出一段时间,但我还没有找到解决方案
要克服该特定错误,您需要允许 :id 和 :article_id 参数
像这样添加它们
params.require(:comment).permit(:author_name, :body, :id, :article_id)
错误NameError in Articles#show undefined local variable or method article
可以通过更改app/views/articles/_comment中的这一行来修复。html.erb(我相信它有_)
来自
<%= link_to 'Delete Comment', article_comment(article, comment), method: :delete, data: { confirm: 'Are you sure?' } %>
到
<%= link_to 'Delete Comment', article_comment_path(comment.article, comment), method: :delete, data: { confirm: 'Are you sure?' } %>
然后在views/articles/show.html.erb中,尝试将collection: @article.comments
更改为collection: @article.comments.all
.
我是 rails 的新手,我已经按照 Jumpstart Labs 的 Blogger 教程制作了一个博客应用程序,我正在尝试实现一项允许登录用户删除评论的功能文章上。 但我一直对这个错误消息感到困惑“ActiveRecord::RecordNotFound in CommentsController#destroy 找不到包含 'id'="
的文章这是我到目前为止所管理的内容,
这是我的 app/controllers/comments_controller.rb
class CommentsController < ApplicationController
before_filter :require_login, except: [:create]
def create
@comment = Comment.new(comment_params)
@comment.article_id = params[:article_id]
@comment.save
redirect_to article_path(@comment.article)
end
def destroy
@article = Article.find(params[:article_id])
@comment = @article.comments.find(params[:id])
@comment.destroy
redirect_to article_path(@article)
end
private
def comment_params
params.require(:comment).permit(:author_name, :body)
end
end
还有我的/views/articles/show.html.erb
<h1><%= @article.title %></h1>
<p>
<b>Posted on <%= @article.created_at.strftime("%B %d %Y") %></b>
</p>
<p>
<% if @article.image.exists? %>
<%= image_tag @article.image.url%>
<% end %>
</p>
<p><%= @article.body %></p>
<%= render partial: 'comments/form' %>
<% if logged_in? %>
<%= link_to "Edit", edit_article_path(@article) %>
<%= link_to "delete", article_path(@article), method: :delete%>
<% end %>
<%= link_to "<< Back to Articles List", articles_path %>
<h3>Comments (<%=@article.comments.size %>) </h3>
<%= render partial: 'articles/comment',
collection: @article.comments %>
<p>
Tags:
<% @article.tags.each do |tag| %>
<%= link_to tag.name, tag_path(tag) %>
<% end %>
</p>
app/views/articles/comment.html.erb
<div>
<p><b>Comment by:</b> <%= comment.author_name %></p>
<p class="comment"><%= comment.body %></p>
<p>Posted <%= distance_of_time_in_words(comment.article.created_at, comment.created_at) %> later</p>
<%= link_to 'Delete Comment', article_comment(article, comment), method: :delete, data: { confirm: 'Are you sure?' } %>
</div>
routes.rb
Blogger::Application.routes.draw do
get 'about', to: 'info#about'
get 'portfolio', to: 'info#portfolio'
get 'contact', to: 'info#contact'
root to: 'articles#index'
resources :articles do
resources :comments
end
resources :tags
resources :authors
resources :author_sessions, only: [ :new, :create, :destroy ]
get 'login' => 'author_sessions#new'
get 'logout' => 'author_sessions#destroy'
end
和我的 articles_controller
class ArticlesController < ApplicationController
include ArticlesHelper
before_filter :require_login, only: [:new, :create, :edit, :update, :destroy]
def index
@articles = Article.all
end
def new
@article = Article.new
end
def create
@article = Article.new(article_params)
@article.save
flash.notice = "Article '#{@article.title}' Created!"
redirect_to article_path(@article)
end
def show
@article = Article.find(params[:id])
@comment = Comment.new
@comment.article_id = @article.id
end
def edit
@article = Article.find(params[:id])
end
def update
@article = Article.find(params[:id])
@article.update(article_params)
flash.notice = "Article '#{@article.title}' Updated!"
redirect_to article_path(@article)
end
def destroy
@article = Article.find(params[:id])
@article.destroy
flash.notice = "Article '#{@article.title}' Deleted!"
redirect_to articles_path
end
结束
非常感谢任何意见或建议,我一直在四处寻找 google 和堆栈溢出一段时间,但我还没有找到解决方案
要克服该特定错误,您需要允许 :id 和 :article_id 参数
像这样添加它们
params.require(:comment).permit(:author_name, :body, :id, :article_id)
错误NameError in Articles#show undefined local variable or method article
可以通过更改app/views/articles/_comment中的这一行来修复。html.erb(我相信它有_)
来自
<%= link_to 'Delete Comment', article_comment(article, comment), method: :delete, data: { confirm: 'Are you sure?' } %>
到
<%= link_to 'Delete Comment', article_comment_path(comment.article, comment), method: :delete, data: { confirm: 'Are you sure?' } %>
然后在views/articles/show.html.erb中,尝试将collection: @article.comments
更改为collection: @article.comments.all
.