未定义的方法 'posts_path'

Undefined Method 'posts_path'

我正在创建一个 rails 应用程序,用户可以在其中创建 posts。不幸的是,视图出了点问题。 Post 索引有效,但是当我尝试创建一个新的 post 时,我收到错误消息“undefined method `posts_path' for #<#<Class:0x007fddd6657258>:0x007fddd39f6d58>" 奇怪的是它引用了行我的表单视图 1,如下所示:

= form_for @post, html: { class: "form form-horizontal post" } do |f|
  .form-group
    = f.label :content, class: 'control-label'
    .controls
      = f.text_area :content, class: 'form-control', required: true
    = error_span(@post[:content])
  = f.submit nil,class: 'btn btn-primary'
  = link_to t('.cancel', :default => t("helpers.links.cancel")), user_posts_path, :class => 'btn btn-default'

它是从我的新视图调用的:

- model_class = Post
.page-header
  %h1=t '.title', :default => [:'helpers.titles.new', 'New %{model}'], :model => model_class.model_name.human.titleize
= render :partial => "form"

我在我的代码中找不到 'posts_path' 的任何实例,这就是我感到困惑的原因。我的路线在这里正确定义:

Rails.application.routes.draw do
  root "users#index"
  resources :users do
    resources :posts
  end
end

如有任何帮助,我们将不胜感激。

你需要 = form_for [@user, @post], html: { class: "form form-horizontal post" } do |f| 因为您的 post 路线嵌套在 user