Clay
Clay

Reputation: 159

Two Form_For on One Model

I have the Following

There is No Relationship between any of these Models.

I would like to Have on the Show Template of the Page to have the Form_for's

Is This Possible?

Current Have The Following:

<div id="sidebar">
    <%= form_for (@business) do |f| %>
     <div id="contact_form_name">
     <p>Company</p>
     <%= f.text_field :company_name, :class =>'form_input_small'  %>
     <%= f.submit 'Submit', :class => 'button' %>
    <% end %>
</div>

<div id="sidebar">
    <%= form_for (@client) do |f| %>
     <div id="contact_form_name">
     <p>First Name</p>
     <%= f.text_field :first_name, :class =>'form_input_small'  %>
     <%= f.submit 'Submit', :class => 'button' %>
    <% end %>
</div>

Error I am Getting in the Log is the following

 <div id="sidebar">
    78:                                 <%= form_for (@business) do |f| %>
    79:                                         <div id="contact_form_name">
    80:                                         <p>Company</p>
    81:                                         <%= f.text_field :company_name, :class =>'form_input_small'  %>
  app/views/pages/show.html.erb:78:in `_app_views_pages_show_html_erb___1556847543_65073939124600'
  app/controllers/pages_controller.rb:9:in `show'

Routes

 businesses GET    /businesses(.:format)                      {:action=>"index", :controller=>"businesses"}
                           POST   /businesses(.:format)                      {:action=>"create", :controller=>"businesses"}
              new_business GET    /businesses/new(.:format)                  {:action=>"new", :controller=>"businesses"}
             edit_business GET    /businesses/:id/edit(.:format)             {:action=>"edit", :controller=>"businesses"}
                  business GET    /businesses/:id(.:format)                  {:action=>"show", :controller=>"businesses"}
                           PUT    /businesses/:id(.:format)                  {:action=>"update", :controller=>"businesses"}
                           DELETE /businesses/:id(.:format)                  {:action=>"destroy", :controller=>"businesses"}

Upvotes: 0

Views: 90

Answers (1)

Noz
Noz

Reputation: 6346

Rails doesn't really care where you have your forms so long as you provide it with the necessary information, & there's nothing that says you can't mingle various models together into a single view.

Assuming you're making use of RESTful resources(as you should), you'll have something like:

resources :pages
resources :companies
resources :clients

Setup in your routes.rb, this makes it pretty easy to specify how you want your form_fors to operate.

For instance on your show action for your Page model you could have something like:

<h1>New Company:</h1>
<%= form_for @company, :url => companies_path do |f| %>
...
<% end %> 

<h1>New Client:</h1>
<%= form_for @client, :url => clients_path do |f| %>
...
<% end %>

Make sure you're setting the instance variables @company and @client in you pages controller show action like @company = Company.new & @client = Client.new.

In both of these cases your forms will post to the create action of their respective models. You can check out relying on record identification for further reading.

Upvotes: 1

Related Questions