I have a single for that needs to save to separate tables in my
database, and create referential IDs for my has_many and belongs_to
models (everything belongs_to the 'entry'). The code that I have
written is below. WARNING: it's ugly.
I need to validate the presence of each of these forms, thus the
terribly long line that checks for all of the saving. However, the
problem with this, is that if something fails along the line, the
other items have already been saved. Not what I want to happen.
Is there a way to simultaneously save and check validations for fields
connected to multiple tables? Is there an easier way to do this in
general? I have a feeling that I don't get how foreign keys are
supposed to function (I'm new to Ruby and application programing in
return unless request.post?
@entry = Entry.new(params[:entry])
@route = Route.new(params[:route])
@destination = Destination.new(params[:destination])
@entry_date = EntryDate.new(params[:entry_date])
@person = Person.new(params[:person])
if @entry.save && @route.save && @destination.save &&
@entry_date.save && @person.save
flash[:notice] = 'Entry was successfully created.'
redirect_to :action => 'index'
render :action => 'index'
<% form_tag :action => 'index' do %>
<%= text_field 'entry_date', 'when' %>
<%= text_field 'route', 'how' %>
<%= text_field 'destination', 'where' %>
<%= text_field 'person', 'what' %>
<%= text_area 'entry', 'body' %>
<%= submit_tag "Create" %>
<% end %>
Thank you for any help in advance. I've been running my head into this
for far too long, and it seems like it should be a lot simpler than it
has turned out to be.