找不到注释控制器的操作'show'如何处理?



我在许多其他线程中搜索,但没有任何帮助。因此,我实现了具有多态性的评论功能,以便每个人都能够对每条评论进行评论。我的问题是,如果我创建评论,他要求显示方法。

我的日志说

Started POST "/comments/59/comments" for 127.0.0.1 at 2018-01-07 21:15:40 
+0100
Processing by CommentsController#create as HTML
User Load (0.5ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? 
ORDER 
BY "users"."id" ASC LIMIT ?  [["id", 1], ["LIMIT", 1]]
Comment Load (0.0ms)  SELECT  "comments".* FROM "comments" WHERE 
"comments"."id" = ? LIMIT ?  [["id", 59], ["LIMIT", 1]]
(0.5ms)  begin transaction
Comment Load (0.0ms)  SELECT  "comments".* FROM "comments" WHERE 
"comments"."id" = ? LIMIT ?  [["id", 59], ["LIMIT", 1]]
SQL (1.0ms)  INSERT INTO "comments" ("body", "created_at", "updated_at", 
"user_id", "commentable_id", "commentable_type") VALUES (?, ?, ?, ?, ?, ?)  
[["body", "huihuihuihuihuihio"], ["created_at", "2018-01-07 
20:15:40.254229"], ["updated_at", "2018-01-07 20:15:40.254229"], 
["user_id", 1], ["commentable_id", 59], ["commentable_type", "Comment"]]
(89.3ms)  commit transaction
(0.0ms)  begin transaction
(0.0ms)  commit transaction
Redirected to http://localhost:3000/comments/60
Completed 302 Found in 118ms (ActiveRecord: 91.3ms)

Started GET "/comments/60" for 127.0.0.1 at 2018-01-07 21:15:40 +0100
AbstractController::ActionNotFound (The action 'show' could not be found 
for CommentsController):

我的路线.rb

resources :posts do 
resources :comments 
end
resources :comments do
resources :comments
end

我的comments_controller.rb

class CommentsController < ApplicationController
before_action :find_commentable

def new
@comment=Comment.new
end

def create 
@comment =@commentable.comments.create(params[:comment].permit(:name, :body).merge(user: current_user))
if @comment.save
redirect_to comment_path(@comment), notice: 'Dein Kommentar wurde gespeichert'
else
redirect_to comment_path(@comment), notice: 'Dein Kommentar konnte leider nicht gespeichert werden, ueberpruefen sie ihre Eingaben'
end
end
private
def find_commentable
@commentable = Comment.find_by_id(params[:comment_id]) if params[:comment_id]
@commentable = Post.find_by_id(params[:comment_id]) if params[:post_id]
end
end

我的评论.rb

class Comment < ApplicationRecord
belongs_to :commentable, polymorphic: true
belongs_to :user
has_many :comments, as: :commentable
end

老实说,我在删除我的帖子和 posts_controller.rb.那里我通过为 show 方法提供与我的删除方法相同的代码来"修复"它。 因此,如果您有想法或需要更多代码,请告诉我

编辑帖子.rb

class Post < ApplicationRecord
belongs_to :user, optional: true
has_many :comments, as: :commentable, dependent: :destroy
validates :title, presence: true, length: {minimum: 5}
validates :body, presence: true
has_attached_file :image  #, :styles => { :medium => "300x300>", :thumb => 
"100x100>" }
validates_attachment_content_type :image, :content_type => /Aimage/.*Z/
end

Post_controller

class PostsController < ApplicationController
def index
@posts = Post.all.order("created_at DESC")
end
def new
@post = Post.new
end
def create
if current_user
@post.user_id = current_user.id
end
@post= Post.new(post_params)
if @post.save
redirect_to @post
else
render 'new'
end
end
def show
@post = Post.find(params[:id])
end
def edit
@post = Post.find(params[:id])
end
def update
@post = Post.find(params[:id])
if @post.update(post_params)
redirect_to @post
else
render 'edit'
end
end
def destroy
@post = Post.find(params[:id])
@post.destroy
redirect_to posts_path
end
private
def post_params
params.require(:post).permit(:title, :body, :theme)
end

end

好吧,您的评论控制器中没有show操作,但您说redirect_to comment_path(@comment)要求服务器提供默认路径"/comments/60"。您为路由提供了资源,因此服务器期望该路由与 show 操作相同。您应该查看所有路线,以便更好地了解正在发生的事情。约定重于配置的想法是假设这些默认值,直到您覆盖它们。当你redirect_to comment_path(@comment)时,你期望会发生什么?

在浏览器中尝试此路径,插入应用名称:

http://localhost/your_app_name/rails/info/routes.

这将向您显示服务器的期望。如果您需要解释,可以将结果添加到您的问题中。但基本上,您需要将该操作添加到控制器,或者告诉控制器在操作完成时呈现其他内容。您希望在该操作完成后看到什么?

如果您不想执行show操作,可以执行以下操作:

redirect_to post_path(@comment.commentable_id)

从下面可以清楚地看出,您的后控制器没有show操作。从您添加的代码来看,您已经这样做了?此外,如果我上面建议的redirect...不起作用,因为没有与@comment相关的post_id,那么您还有其他更深层次的问题。你们中的大多数问题都源于您正在使用标准的东西,例如

resources :posts do 
resources :comments 
end

但是,然后执行非标准操作,例如没有为每个控制器提供标准的 CRUD 操作。是的,你可以跳过一些你不需要的东西,但 Rails 的目的是帮助简化约定而不是配置。

编辑

post_id更改为commentable_id,因为这是代码中引用它的方式。这可能有效,但由于您允许对评论发表评论,因此可能不行。基本上,如果评论属于属于帖子的评论,那么您必须向上遍历该树,或者像您选择的那样转到索引页面。

最新更新