訪問PARAMS我有以下型號Rails的:形式與嵌套模式
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
我有一個新用戶的形式組織,以嵌套模式gym_user。我想確保用戶不存在。這是我想要的:
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
我知道這裏只有一個嵌套模型,但我不知道如何訪問這些嵌套參數。
這裏的形式本身
<%= 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 %>
這是一個多對多的,所以它不能HAS_ONE – Jhorra
我真正需要知道的是如何訪問這些參數。 – Jhorra
他們應該在您的開發日誌中打印出來。在你的控制器中也是params.inspect –