例如:我可以渲染 '/tags/:id/posts' 吗?
如果我尝试渲染 tag_posts_path(@tag) 或其他方式,我会收到此错误或类似错误:
Missing partial /tags/1/posts...
在我的 routes.rb 我有这个:
resources :tags do
resources :posts
end
例如:我可以渲染 '/tags/:id/posts' 吗?
如果我尝试渲染 tag_posts_path(@tag) 或其他方式,我会收到此错误或类似错误:
Missing partial /tags/1/posts...
在我的 routes.rb 我有这个:
resources :tags do
resources :posts
end
我有点像你这样的东西
我实现了渲染正确的页面
def create
@project = Project.find(params[:project_id])
@comment = @project.comments.build(comment_params)
if @comment.save
flash[:success] = "Chingon!"
redirect_to @project
else
render 'projects/show'
end
end
我的路线看起来像:
resources :projects do
resources :comments, only: [:create, :destroy]
end
Rails 期望您呈现的不是路径而是某些页面(例如“新”或“显示”)我只在渲染“项目/显示”的 ELSE 时遇到了问题。它似乎不是很正确的模板,因为它以原始形式呈现,没有任何 CSS,但我预计会出现错误。这是目前我可以为空注释呈现错误消息的唯一方法。
我只是通过绕过默认方式来显示错误并将其放在简单的 Flash 中,如下所示:
def create
@project = Project.find(params[:project_id])
@comment = @project.comments.build(comment_params)
if @comment.save
flash[:success] = "New comment here, bro"
redirect_to @project
else
redirect_to @project
flash[:error] = flash_error_message(@comment)
end
end
def flash_error_message(arg)
"The form contains #{arg.errors.count} error: #{arg.errors.full_messages.join(', ')}"
end
如果我是正确的,我认为你正在寻找的是这样的
resources :tags do
resources :posts
end
您可以通过输入来查看生成的 URL 或路由类型rake routes
我不太确定,但是,重新阅读您的问题,我认为您的意思是说
render 'some nested route'
我提供的上述路线将允许你做这样的事情......
render tag_posts_path(@tag)
这将在术语中寻找索引操作,因为它的帖子路径。再次,rake routes
展示了这一切。
在谷歌上快速搜索嵌套路线会引导你到这个。