Saving belongs_to associations

I'm trying to save a username along with profiles (User has_many
Profiles; Profile belongs_to User). I had a portion of this working
(updating user info) but I cannot create new user records.
I get the following error:

You have a nil object when you didn't expect it!
The error occurred while evaluating

{"commit"=>"Create record",

I am getting stuck on the profile model code below:

  # Users
  def new_user_attributes=(user_attributes)
    user_attributes.each do |attributes|
  def existing_user_attributes=(user_attributes)
    user.reject(&:new_record?).each do |user|
      attributes = user_attributes[]
      if attribute
        user.attributes = attributes
  def save_user

New method in profiles_controller:
  def new
    @profile =
    @profile.user =

User partial:
<div class="user">
  <% new_or_existing = user.new_record? ? 'new' : 'existing' %>
  <% prefix = "profile[#{new_or_existing}_user_attributes][]" %>
  <% fields_for prefix, user do |user_form| -%>
    <%= label :user_form, :user, "Username" %><%=
user_form.text_field :username %>
    <%= user_form.hidden_field :active, :value => "1" %>
  <% end -%>