渲染simple_form错误
问题描述:
我有列表(页面#首页),点击我打开列表与评论(职位#显示),其中我声明变量@feed_items。在评论结束时,我对新评论有了simple_form(评论#创建)。问题:如果提交按钮发生错误,我需要渲染带有注释的表单和带有错误的表单。 我想在评论#创建:渲染simple_form错误
if @comment.save
...
else
render 'posts/show'
end
但在这种情况下,变量@feed_items没有声明是因为方法的帖子#秀没叫。当我尝试写入redirect_to时,我会看到包含注释但没有错误消息的列表。这个怎么做?
答
这是人们常犯的错误。 render
方法只选择要显示的视图,并且redirect_to
方法再次执行重定向的操作。
当出现错误时,您可以调用render
方法来选择要显示的视图,并且必须手动分配此视图需要解决的所有实例变量。
检查Section 2.3.2
从铁轨指南:http://guides.rubyonrails.org/layouts_and_rendering.html
在你的情况下,它会是这样的:
if @comment.save
...
else
# Populate the @feed_items in here again
render 'posts/show'
end