Topic: multimodel form woes, and booleans

I've been browsing the tutorials for saving multiple models with a single form, as it seems quite a popular problem some, including me, are facing. The thing that makes this difficult in my case is that I have an existing parent model that the two new ones relate to, and the tutorials seem to only be about creating two new models, as far as I've seen.

on to the relations:

album has_many :volumes
volume belongs_to :album
volume has_one :dimension
dimension belongs_to :volume

  def create
    @album = Album.find(params[:id])
    @volume = @album.volumes.create(params[:volume])
    @dimension = @volume.dimension.build(params[:dimension])
    if @volume.save
      redirect_to :action => 'show', :id => @volume
      flash[:notice] = "Successfully created..."
    else
      render :action => 'new', :id => @album
    end
  end

and heres the form:
<%=start_form_tag :action => 'create', :id => @album%>
<%= error_messages_for :volume%>
<%=error_messages_for :size%>
<table border="0" cellspacing="5" cellpadding="5">
    <tr><td>volume title:</td><td><%=text_field :volume, :vol_title%></td></tr>
    <tr><td>volume number:</td><td><%=text_field :volume, :vol_number, :size => 10%></td></tr>
    <tr><td>Pages:</td><td><%=text_field :volume, :pages, :size => 10%></td></tr>
    <tr><td>width:</td><td><%=text_field :dimension, :width, :size => 5%> cm</td></tr>
    <tr><td>height:</td><td><%=text_field :dimension, :height, :size => 5%> cm</td></tr>
    <tr><td>thickness:</td><td><%=text_field :dimension, :thickness, :size => 5%> cm</td></tr>
    <tr><td><%=submit_tag 'Submit'%></td></tr>               
</table>
<%=end_form_tag%>

the create action throws a nil.build error on submit, but the volume is still created successfully.

also on a related note, I've been wondering what the best way to deal with boolean values in forms, that would actually reflect on different ones to the end-user, for example if I had a boolean "read_direction" where 0 would be left-to-right, and 1 right-to-left, and I'd want to show this in the form instead of the raw value so the user doesnt have to guess, the new form is easy to hack around, but I'm wondering if theres a solution so I could use the same form for my edit action and still get the boolean autocompleted?

Re: multimodel form woes, and booleans

sgr wrote:

the create action throws a nil.build error on submit, but the volume is still created successfully.

has_one relationships are handled a bit differently. Try:

@volume = @album.volumes.new(params[:volume])
@dimension = @volume.build_dimension(params[:dimension])

sgr wrote:

also on a related note, I've been wondering what the best way to deal with boolean values in forms, that would actually reflect on different ones to the end-user, for example if I had a boolean "read_direction" where 0 would be left-to-right, and 1 right-to-left, and I'd want to show this in the form instead of the raw value so the user doesnt have to guess, the new form is easy to hack around, but I'm wondering if theres a solution so I could use the same form for my edit action and still get the boolean autocompleted?

Try a checkbox:

<%= check_box :my_model, :my_attr %>

Railscasts - Free Ruby on Rails Screencasts

Re: multimodel form woes, and booleans

ah, works perfect now, havent had to use has_one relations until now so I just assumed they'd be similar to has_many, good to know for the future.

the checkbox approach works fine, and I'm currently using it, what I was wondering is if there was a select helper that'd create a dropdown with model values, but that I could assign custom option names to, so the :album, :direction boolean could result in

<option value="false">L to R</option> 
<option value="true">R to L</option>

Re: multimodel form woes, and booleans

Try this:

<%= select :album, :direction, [['L to R', 0], ['R to L', 1]] %>

Railscasts - Free Ruby on Rails Screencasts

Re: multimodel form woes, and booleans

works like a charm, as long as the values are true/false instead of 0/1, thanks smile