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