saving to multiple models from one form with rails

brent picture brent · Feb 18, 2012 · Viewed 7.2k times · Source

i have been getting into rails over the course of the last couple weeks and i've run across a problem that I can not find the answer to. i feel like this is probably something really simple but it's driving me crazy so I am hoping to get a little bit a guidance...

I am inserting data from 1 form into 2 models. one model is called Venue and one model is called Events. It is a one to many association where multiple events belong to venues and venues have many events.

models:

class Venue < ActiveRecord::Base
  has_many :events
  accepts_nested_attributes_for :events
end

class Event < ActiveRecord::Base
  belongs_to :venue

  scope :upcoming, where('date >= ?', Date.today)
 end  

controller:

class EventsController < ApplicationController

def new
      @venue = Venue.new
end

def create

@venue = Venue.new(params[:venue])

    if @venue.save
        render :inline => "Success"
    else
        render('new')
    end
end

end

form:

<%= form_for(@venue, :url =>{:action => 'create'}) do |f| %>

        Artist ID<br />
        <%= fields_for :event do |event_fields| %>
            <%= event_fields.text_field :artist_id %><br/><br />
        <% end %>

        Venue City<br />
        <%= f.text_field(:city) %> <br /><br />
        Venue Name<br />
        <%= f.text_field(:name) %><br/><br/>


        <div class="actions">
            <%= submit_tag "Save", :class => "btn primary" %>
        </div>

<% end %>

log output:

Started POST "/events" for 127.0.0.1 at 2012-02-17 19:57:24 -0500
Processing by EventsController#create as HTML
Parameters: {"utf8"=>"✓",   "authenticity_token"=>"p3llx5KsYn6gyjP9g2qwzXr+0rjh3h/o34h/iqvqjRo=", "event"=>{"artist_id"=>"124"}, "venue"=>{"city"=>" Boston", "name"=>"Bostons Fa'v"}, "commit"=>"Save"}
(0.2ms)  BEGIN
SQL (0.8ms)  INSERT INTO "venues" ("address_1", "address_2", "capacity", "city", "country", "created_at", "created_by", "name", "state", "updated_at", "url", "zip") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12) RETURNING "id"  [["address_1", nil], ["address_2", nil], ["capacity", nil], ["city", " Boston"], ["country", nil], ["created_at", Sat, 18 Feb 2012 00:57:24 UTC +00:00], ["created_by", nil], ["name", "Bostons Fa'v"], ["state", nil], ["updated_at", Sat, 18 Feb 2012 00:57:24 UTC +00:00], ["url", nil], ["zip", nil]]
(0.4ms)  COMMIT
Rendered inline template (0.2ms)
Completed 200 OK in 5ms (Views: 0.6ms | ActiveRecord: 1.4ms)

As you can see its not failing but it is only submitting to the 1 model. My understand is that by adding "accepts_nested_attributes_for :events" the Venue model knows to then go to my Events table, create a new event with the event data I provide (in this example just artist_id), and then automatically insert the id field from my venue table into venue_id.

The more I think about this here the more I think I might be missing a step and....If anyone has a second I would appreciate hearing your thought process on this.

Thanks

Answer

bruno077 picture bruno077 · Feb 18, 2012

I think you have a syntax error in the form. The fields_for is a method of f and it expects a plural symbol. It is also a good practice to have the iterator of a block in singular, and not to use parentheses when your method only expects one parameter:

<%= form_for(@venue, :url =>{:action => 'create'}) do |f| %>

        Artist ID<br />
        <%= f.fields_for :events do |event_field| %>
            <%= event_field.text_field :artist_id %><br/><br />
        <% end %>

        Venue City<br />
        <%= f.text_field :city %> <br /><br />
        Venue Name<br />
        <%= f.text_field :name %><br/><br/>


        <div class="actions">
            <%= submit_tag "Save", :class => "btn primary" %>
        </div>

<% end %>

See this example of a nested form in Ryan Bate's Railscasts:

<%= form_for @survey do |f| %>
  <%= f.error_messages %>
  <p>
    <%= f.label :name %><br />
    <%= f.text_field :name %>
  </p>
  <% f.fields_for :questions do |builder| %>
  <p>
    <%= builder.label :content, "Question" %><br />
    <%= builder.text_area :content, :rows => 3 %>
  </p>
  <% end %>
  <p><%= f.submit "Submit" %></p>
<% end %>