我有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
我将行resources :invoices, only: [:new]
添加到routes.rb
文件,将<%= link_to 'New Invoice', new_invoice_path(@invoice) %>
添加到aplication.html.erb
但是,我已将new
操作设置为仅在invoice
route
/clients/:client_id/invoices/new
时才有效
invoicesController
def new
@client = Client.find(params[:client_id])
@invoice = @client.invoices.new
end
我的表格
<%= simple_form_for [@client, @invoice] do |f| %>
如果我将控制器的新操作更改为
def new
@invoice = Invoice.new
end
我得到undefined method invoices_path for #
我知道这是来自我设置form
如何使两个路径都能在一个控制器操作中运行?我应该有两种表格吗?
答案 0 :(得分:0)
你还需要启用:在路径中创建(form_for将通过post方法链接到哪里)并具有控制器操作
def create
处理表单的输入。否则它将无法工作。