Ruby on rails ActionController::CommentsController中的ActionController错误#创建
下面是控制器Ruby on rails ActionController::CommentsController中的ActionController错误#创建,ruby-on-rails,Ruby On Rails,下面是控制器 class CommentsController < InheritedResources::Base def comment_params params.require(:comment).permit(:name, :email, :body, :post_id) end def create @comment = Comment.new(params[:comment_params]) i
class CommentsController < InheritedResources::Base
def comment_params
params.require(:comment).permit(:name, :email, :body, :post_id)
end
def create
@comment = Comment.new(params[:comment_params])
if @comment.save
flash[:notice] = 'Comment was succesfully posted.'
redirect_to(@comment.post)
else
flash[:notice] = "Error creating comments: #{@comment.errors}"
redirect_to(@comment.post)
end
end
end
通过注释redirect_to in else块将通过另一个错误,即缺少模板
我尝试过以前问过的几个问题的解决方案,但没有任何帮助 即使未保存,您的else块中的用户也会重定向到@comment.post。所以基本上是零。 我不认为你的评论被保存。试着这样做,看看是否有任何错误
if @comment.save! # this will show errors if any
...
else
post = Post.find(params[:post_id])
redirect_to post
end
我想你的情人一定有身份证。如果没有,则@comment.post也将为零。在if和else块中都会导致该错误
else
flash[:notice] = "Error creating comments: #{@comment.errors}"
redirect_to(@comment.post) <--
end
应该是哪一个
@comment = Comment.new(comment_params)
不是
params[:comment\u params]
而是comment\u params
,您应该将create
操作更改为该操作,并确保设置了post\u id
:
class CommentsController < InheritedResources::Base
def comment_params
params.require(:comment).permit(:name, :email, :body, :post_id)
end
def create
@comment = Comment.new(comment_params)
if @comment.save
flash[:notice] = 'Comment was succesfully posted.'
redirect_to(@comment.post)
else
flash[:notice] = "Error creating comments: #{@comment.errors}"
redirect_to(@comment.post)
end
end
end
class CommentsController
你也可以发布你的表单吗?通过注释重定向到else块将通过一个错误:缺少模板注释/create,继承的资源/base/create,应用程序/create和{:locale=>[:en],:formats=>[:html],:variants=>[:erb,:builder,:raw,:ruby,:coffee,:arb,:jbuilder]}。搜索地址:“*”c:/sites/myrubyblog/app/views“*”c:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/designe-4.1.1/app/views“*”c:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/bundler/gems/activeadmin-f0243d495067/app/views“*”c:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/kaminari/17.0/views”>
@comment = Comment.new(comment_params)
class CommentsController < InheritedResources::Base
def comment_params
params.require(:comment).permit(:name, :email, :body, :post_id)
end
def create
@comment = Comment.new(comment_params)
if @comment.save
flash[:notice] = 'Comment was succesfully posted.'
redirect_to(@comment.post)
else
flash[:notice] = "Error creating comments: #{@comment.errors}"
redirect_to(@comment.post)
end
end
end