How to create a nested form in Rails 3 that works? (I get a routing error.)

I have a Client and ProposalRequest model that looks like this:

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

In my routes file, I included nested routes, as usual.

resources :clients do
  resources :proposal_requests
end

And this is my form:

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

But after that I got stuck due to this error.

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>}

Can someone help me deal with this error?

+3
source share
1 answer

The problem is that your nested route is designed to add a new one ProposalRequestto an existing one Client. If you want to create Clientand at the same time ProposalRequest, you just need to use 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 , , .

+2

Source: https://habr.com/ru/post/1779126/


All Articles