I have a User model and an Address model.
A user has_many addresses, and an address belongs_to a user.
An address object for a user could be a billing, shipping or marketing address.
The way I''m tracking this is via the user attributes:
billing_adress_id, marketing_address_id and shipping_address_id in the
user table, which holds the relevant address id.
But in my form that allows a user to edit an address, including
nominating it as a marketing, shipping or billing address, I''m getting
stuck with the check_box helpers setting this up.
Here''s my form (edit_address.rhtml -- you''ll see that I have
some
fields for both user and address objects in the form)
<% form_for :address, :url => { :action => "edit_address" },
:html =>
{:id => ''account_form''} do |f| %>
<dl>
<% fields_for :user, @address.user do |u| %>
<dt><label for="first_name">First
name:</label></dt>
<dd><%= u.text_field :first_name, :size => "30"
%></dd>
<dt><label for="last_name">Last
name:</label></dt>
<dd><%= u.text_field :last_name, :size => "30"
%></dd>
<dt><label for="billing_address_id">Billing
address?:</label></dt>
<dd><%= u.check_box :billing_address_id, {}, @address.id, nil
%></dd>
<dt><label for="shipping_address_id">Shipping
address?:</label></dt>
<dd><%= u.check_box :shipping_address_id, {}, @address.id, nil
%></dd>
<dt><label for="marketing_address_id">Marketing
address?:</label></dt>
<dd><%= u.check_box :marketing_address_id, {}, @address.id, nil
%></dd>
<% end %>
<dt><label for="address1">Street
address:</label></dt>
<dd><%= f.text_field :address1, :size => "30"
%></dd>
<dt><label for="address2">Street address
(ctd):</label></dt>
<dd><%= f.text_field :address2, :size => "30"
%></dd>
</dl>
<%= submit_tag("Update your details", :class =>
"submit") %>
<% end %>
So I want the checkboxes to be checked if
@address.user.billing_address_id = @address.id
But the checkboxes aare checked no matter what value is in the
database. If the attribute is blank, then it remains unchecked as you
would expect, but the checkbox will be checked even if
@address.user.billing_address_id != @address.id
So I''m stumped! I can''t work out how to get this working the
way I want it to.
Any clues appreciated