1
我想构建一个嵌套窗体,它将创建一个新用户并将它们订阅到一个计划。嵌套窗体没有将子对象关联到父项
当我点击“注册”,我得到以下错误:
Validation failed: Plan subscriptions user can't be blank
我有以下双重和三重检查一切,我不知道什么是错在这一点上。任何想法为什么订阅没有被关联到新的用户记录?
这里是我的代码:
User.rb
class User < ActiveRecord::Base
attr_accessible ..., :plan_subscriptions_attributes
has_many :plan_subscriptions, dependent: :destroy
accepts_nested_attributes_for :plan_subscriptions
PlanSubscriptions.rb
class PlanSubscription < ActiveRecord::Base
belongs_to :user
Plan_Subscriptions#新
def new
@plan = Plan.find(params[:plan_id])
@user = User.new
@user.plan_subscriptions.build
end
Plan_Subscriptions/New.html
<%= form_for @user do |f| %>
<fieldset>
<%= f.text_field :first_name, :label => false, :placeholder => 'First Name', :required => false %>
<%= f.text_field :last_name, :label => false, :placeholder => 'Last Name',
<%= f.fields_for :plan_subscriptions do |builder| %>
<%= builder.hidden_field :plan_id, :value => @plan.id %>
<% end %>
<%= f.submit 'Enroll', :error => false %>
</fieldset>
<% end %>
对不起,不应该隐藏那条线。我可以确认'@ plan'是有效的。它根据url参数在控制器中设置。我可以确认'@ plan.id'被设置为“2”(一个有效的ID)。所以这似乎不是。错误是抱怨空白的用户ID。 – pejmanjohn