Rails: Access params of a form with a nested model

2019-06-11 09:52发布

问题:

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 &nbsp;
                    <%= 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 %>

回答1:

I found the answer. I'm not sure if this is the best way but it works

params[:user][:gym_users_attributes].values.first


回答2:

Your association should be

has_one :gym, :through => gym_users

Also, can you post your form paramaters?



回答3:

Your focus is on not allowing creation of duplicate users, and you are using email to verify this. You should rather use the helper

validates_uniqueness_of :email

in your User model. As you have a nested attribute, your object will be created only after Rails validates that a user with same email doesn't exist already.

I hope I understood your problem right.