I have the following models
class GymUser < ActiveRecord::Base
belongs_to :user
belongs_to :gym
end
class User < ActiveRecord::Base
has_many :gym_users
has_one :gym
attr_accessible :gym_users_attributes, :gym_users
accepts_nested_attributes_for :gym_users
end
I have a form for a new user, with a nested model gym_user. I want to make sure the user doesn't exist already. This is what I'm trying:
def create_member
@user = User.new(params[:user])
@user.generate_password
@dupe = User.find_all_by_email(@user.email)
if(@dupe)
@gym_user = GymUser.new(params[:user][:gym_users_attributes])
@gym_user.user_id = @dupe.id
elsif @user.save
@gym_user = @user.gym_users.order('created_at DESC').first
@gym = Gym.find(@gym_user.gym_id)
end
end
I know there's only one nested model here, but I can't figure out how to access those nested parameters.
Here's the form itself
<%= form_for @user, :as => :user, :remote => true, :url => { :controller => 'users', :action => 'create_member'} do |f| %>
<table border="0" cellpadding="10" cellspacing="0">
<tr>
<td colspan="2">
<%= f.label :name %><br />
<%= f.text_field :name %>
</td>
<td colspan="2">
<%= f.label :email %><br />
<%= f.text_field :email %>
</td>
</tr>
<% f.fields_for :gym_users do |builder| %>
<tr>
<td>
<%= builder.label :role_id, "Role" %><br />
<%= builder.collection_select(:role_id, @roles, :id, :name, {:include_blank => true}, {:onchange => "new_member_role_changed()"}) %>
<%= builder.hidden_field :gym_id, :value => @gym.id %>
</td>
<td>
<%= builder.label :item_id, "Membership Level" %><br />
<%= builder.collection_select(:item_id, @gym.membership_items, :id, :name, {:include_blank => true}) %>
</td>
<td>
<%= builder.label :has_monthly_billing, "Recurring Billing?" %><br />
<%= builder.radio_button :has_monthly_billing, "1" %>Yes
<%= builder.radio_button :has_monthly_billing, "0" %>No
</td>
<td>
<%= builder.label :billing_date %><br />
<%= builder.collection_select(:billing_date, (1..31).to_a, :to_s, :to_s, {:include_blank => true}) %>
</td>
</tr>
<tr>
<td colspan="4">
<%= f.submit %>
<a href="javascript:close_new_member()">Cancel</a>
</td>
</tr>
<% end %>
</table>
<% end %>