Validation Rails视图:在使用嵌套资源验证失败后调用呈现后缺少模板

Validation Rails视图:在使用嵌套资源验证失败后调用呈现后缺少模板,validation,rest,ruby-on-rails-3,controller,render,Validation,Rest,Ruby On Rails 3,Controller,Render,我正在尝试处理Rails3中失败的验证 我有一个带有嵌套注释的章节资源。总共只有一个视图;来自chapters_controller的show视图,其中包含用于发布新注释的表单,并指向注释_controller创建操作,如下所示(包括控制器代码): 验证失败时,它会错误地路由到:“/chapters/1/comments”,并在页面上显示以下内容: Template is missing Missing template http://localhost:3000/chapters/1 wi

我正在尝试处理Rails3中失败的验证

我有一个带有嵌套注释的章节资源。总共只有一个视图;来自chapters_controller的show视图,其中包含用于发布新注释的表单,并指向注释_controller创建操作,如下所示(包括控制器代码):

验证失败时,它会错误地路由到:“/chapters/1/comments”,并在页面上显示以下内容:

Template is missing Missing template http://localhost:3000/chapters/1 with {:locale=>[:en, :en], :formats=>[:html], :handlers=>[:rhtml, :rxml, :builder, :erb, :rjs]} in view paths "/app/views", "", "/" 缺少模板 缺少模板http://localhost:3000/chapters/1 在视图路径“/app/views”中使用“{:locale=>[:en,:en],:formats=>[:html],:handlers=>[:rhtml,:rxml,:builder,:erb,:rjs]}” 干杯


Paul

我认为问题在于渲染调用:

if @comment.save
  redirect_to chapter_url(params[:chapter_id])
else
  render chapter_url(params[:chapter_id])
end
我认为传递URL进行渲染是不正确的(请参见此处的api:)。通常你打电话

render :action => new
这将呈现当前控制器的新视图。由于您希望呈现章节“show”视图(即不同控制器的视图),因此我认为调用应该更像:

render :template => 'chapters/show'

我对Rails的了解有些生疏,Rails3可能会改变一切。希望这对将来的人有所帮助。

对于那些人,我只是遇到了一个类似的问题

当地址创建操作的验证失败时,我正在尝试呈现:

render new_user_address_path(@user)
#Normally this should work with a redirect_to or a link_to but render doesn't work this way
相反,你应该:

render 'new'

正如@ideaasylum所提到的,您不能呈现“命名助手”,但可以呈现动作。对我来说,这似乎是rails的一种特质。从技术上讲,命名路径和操作应该位于同一位置。

重定向后,必须立即返回,因为redirect\u to不会返回

 if @comment.save
      redirect_to chapter_url(params[:chapter_id]) and return
    else
      render chapter_url(params[:chapter_id]) and return
    end

谢谢Jamie,不幸的是,它仍然重定向到“章节/1/评论”我有点困惑。。。