我正在尝试用Rails4做一个应用程序。
我有一个文章模型和一个评论模型。
这些关联包括:
article.rb
has_many :comments, as: :commentable, dependent: :destroy
accepts_nested_attributes_for :commentscomment.rb
belongs_to :commentable, :polymorphic => true在我的文章show view中,我有:
<div class="row">
<div class="col-sm-9">
<div class="intpol3" style="text-align: left; margin-left:60px; padding-top:50px">
<%= safe_join(@article.body.split("\r\n"), "<br />".html_safe) %>
</div>
</div>
<div class="col-sm-3">
<!-- placeholder for tags -->
</div>
</div>
<!-- if @article.user.full_name.present? -->
<!-- <div class="indexsubtext"> @article.user.full_name </div> -->
<!-- end -->
<div class="row">
<div class="col-xs-8">
</div>
<div class="col-xs-4">
<div class="formminor" style="margin-bottom:5%">
<%= link_to 'Edit', edit_article_path(@article) %> |
<%= link_to 'More from the blog', articles_path %>
</div>
</div>
</div>
<div class="row">
<div class="col-xs-12">
<%= render :partial => 'comments/form', locals: {commentable: @article} %>
</div>
</div>
<div class="row">
<div class="col-xs-12">
<div class="intpol3">
<%= render :partial => 'comments/display', locals: {commentable: @article} %>
</div>
</div>
</div> 在我的评论显示部分中,我有:
<div class="row">
<div class="col-xs-11">
<% commentable.comments.each do | comment | %>
<div class="well">
<%= comment.opinion %>
<div class="commentattributionname">
<%= comment.user.full_name %>
</div>
<div class="commentattributiontitle">
<%= comment.user.formal_title %>
</div>
<div class="commentattributiondate">
<%= comment.created_at.try(:strftime, '%e %B %Y') %>
</div>
</div>
<div class="col-xs-1">
<%= button_to 'Delete', commentable.comment, :method => :delete %>
</div>
<% end %>这一切都正常,直到我尝试添加一个按钮来删除评论。评论模型是多态的。
我有两个用于注释的控制器(遵循gorails.com教程),第一个是文章::注释控制器,它具有:
class Articles::CommentsController < CommentsController
before_action :set_commentable#, only: [:show, :edit, :update, :destroy]
private
# Use callbacks to share common setup or constraints between actions.
def set_commentable
@commentable = Article.find(params[:article_id])
end
end第二个是comments控制器,它具有:
class CommentsController < ApplicationController
before_action :authenticate_user!
def create
@comment = Comment.new comment_params
@comment.user = current_user
@comment.commentable = @commentable
respond_to do |format|
if @comment.save
format.html { redirect_to @commentable }
format.json { render :show, status: :created, location: @comment }
else
format.html { render :new }
format.json { render json: @comment.errors, status: :unprocessable_entity }
end
end
end
def destroy
@comment.destroy
respond_to do |format|
format.html { redirect_to data_url }
format.json { head :no_content }
end
end
private
# Never trust parameters from the scary internet, only allow the white list through.
def comment_params
params.require(:comment).permit(:opinion)
end
end我想试着删除评论,而不是文章。当我尝试这样做时,错误消息告诉我注释控制器中的销毁操作是有问题的。
文章控制器中的销毁操作是:
def destroy
before_action :authenticate_user!
authorize @article
@article.destroy
respond_to do |format|
format.json { head :no_content }
end
end错误消息为:
ActionController::UnknownFormat in ArticlesController#destroy
ActionController::UnknownFormatrespond_to行将突出显示。我不知道这些词是什么意思-当你使用脚手架创建控制器时,它们是自动插入的。但是,我在控制器中添加了一个respond to格式:
respond_to :html, :son我不确定为什么我删除评论的尝试被定向到了文章控制器中的销毁操作。
发布于 2016-01-03 18:05:31
您不应该有两个注释控制器(Articles::Comments和Comments);您应该坚持使用Articles::Comments,并将其嵌套在路由中的Articles下:
#config/routes.rb
resources :articles do
resources :comments, module: :articles #-> url.com/articles/:article_id/comments/:id
end
#app/controllers/articles/comments_controller.rb
class Articles::CommentsController < ApplicationController
before_action :set_commentable
def destroy
@comment = @article.comments.find params[:id]
@comment.destroy
end
private
def set_commentable
@article = Article.find params[:article_id]
end
end这应该允许您使用以下路由:
<%= button_to 'Delete', [commentable, comment], method: :delete %>--
我认为您对nested resources感到困惑--每当您设置嵌套在其他路由下的路由时,您都有机会将“父”和“子”资源传递给您的路由。
这在form_for和link_to / button_to中都有效--您必须传递["parent.id", "child.id"]才能使嵌套路由正常工作。
-
另一个好技巧是使用polymorphic_path,它将根据您传递的对象自动设置路由。我并没有经常使用它,但是在这个例子中看起来你可以从中受益:
<%= button_to 'Delete', polymorphic_path([commentable, comment]), method: :delete %>https://stackoverflow.com/questions/34572024
复制相似问题