我有一个"new"表单,可以在帖子模型中进行验证。当验证器启动时,它会错误地呈现。
新的帖子页面路径位于“/posts/new”
验证时,新的帖子页面路径位于“/posts”。我需要它返回“/posts/new”。
这是我的 Controller :
def create
@post = current_user.posts.build(params[:post])
if @post.save
redirect_to public_post_page_path(@post.public_url)
else
render :action => :new
end
end
我有一种感觉,这可能与我的形式有关。所以这是格式化的第一行:
<%= semantic_form_for [:student, post], :html => {:id => "post_form"} do |form| %>
最佳答案
这是 Rails 的正确行为。
在创建操作中,它只是渲染"new" View 文件。因此,url 将为/posts,但 View 将正确显示表单。这种行为并没有什么问题;一般来说,Rails 约定是很好的形式。如果您只是渲染新的,内置的 Rails 错误也会起作用;但是,如果您重定向它们将不会显示。
如果您确实想返回到该网址,则需要使用:
redirect_to
而不是渲染。
关于ruby-on-rails - 渲染:new not going to the right place after a validation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8775599/