Ruby on rails rails中的表单和嵌套资源

Ruby on rails rails中的表单和嵌套资源,ruby-on-rails,forms,nested-resources,Ruby On Rails,Forms,Nested Resources,我有4个模型用户,客户,发票和项目 当前,用户可以单击客户机并查看该客户机的所有发票。但是,当他选择创建新发票时,新发票已分配给客户。我还希望用户能够创建一个新的发票,并从列表中选择一个客户或创建一个新的 路线与模型 devise_for :users resources :invoices, only: [:new] resources :clients do resources :invoices, shallow: true end class User < ActiveRecor

我有4个模型用户,客户,发票和项目

当前,用户可以单击客户机并查看该客户机的所有发票。但是,当他选择创建新发票时,新发票已分配给客户。我还希望用户能够创建一个新的发票,并从列表中选择一个客户或创建一个新的

路线与模型

devise_for :users
resources :invoices, only: [:new]
resources :clients do
resources :invoices, shallow: true
end

class User < ActiveRecord::Base
has_many :invoices
has_many :clients

class Client < ActiveRecord::Base
has_many :invoices
has_many :items, through: :invoices

class Invoice < ActiveRecord::Base
belongs_to :client
has_many :items, :dependent => :destroy

class Item < ActiveRecord::Base
belongs_to :invoice
belongs_to :client
还有我的表格

<%= simple_form_for [@client, @invoice] do |f| %>
我得到了35;的
未定义方法路径#
我知道这来自我设置
表单的方式


我如何使两条路由在一个控制器操作下工作?我应该有两个表单吗?

您还需要启用:在routes中创建(其中form_for将通过post方法链接到)并具有控制器操作

def create

处理表单的输入。否则它将不起作用。

您没有显示发票路径所在的代码…运行
rake routes
并确保该路径是发票的正确帮助器…因为您嵌套了该路径,它很可能不是。该行
参考资料:发票,仅:[:新建]
嵌套资源块外部创建路由new\u invoice GET/invoices/new(:format),但错误是未定义方法
invoices\u path
!这就是为什么添加“创建固定”的原因。谢谢,我在我的路线中添加了
资源:发票,仅:[:新建,:创建]
,并将我的表单更改为
,它按预期工作
def new
 @invoice = Invoice.new
end
def create