3

我有一个看起来像这样的 Client 和 ProposalRequest 模型:

class Client < ActiveRecord::Base
  has_many :proposal_requests
  accepts_nested_attributes_for :proposal_requests, :allow_destroy => true
end

class ProposalRequest < ActiveRecord::Base
  belongs_to :client

end

在我的路线文件中,我像往常一样包含了嵌套路线。

resources :clients do
  resources :proposal_requests
end

到目前为止,这是我的表格:

-semantic_form_for [Client.new, ProposalRequest.new] do |f|
   =f.inputs
   =f.buttons

但在此之后,我因为这个错误而被卡住了。

No route matches {:controller=>"proposal_requests", :client_id=>#<Client id: nil, name: nil, title: nil, organization: nil, street_address: nil, city: nil, state: nil, zip: nil, phone: nil, email: nil, status: "interested", how_you_heard: nil, created_at: nil, updated_at: nil>}

谁能帮我解决这个错误?

4

1 回答 1

2

问题是您的嵌套路由旨在将新路由添加ProposalRequest到现有的Client. 如果你想同时创建 aClient和 a ProposalRequest,你只需要使用new_client_pathand semantic_form_for @client do |f|

我建议您在您的clients_controller:

def new
  @client = Client.find(params[:id])
  @client.proposal_requests.build
end

在您看来:

semantic_form_for @client do |f|      
  = f.inputs # fields for client
  = f.inputs :name => 'Proposal Request', :for => :proposal_requests do |pf|
    = pf.input :some_proposal_request_attribute
  = f.buttons

希望这可以帮助。请务必查看https://github.com/justinfrench/formtastic上的所有示例,并进行一些试验和错误以使您的表单达到您想要的效果。

于 2010-12-08T06:46:25.553 回答