2015-05-18 58 views
0

我添加了一個嵌套的屬性到我的表單中,嵌套屬性是教育字段的字段不呈現,其他字段都是這樣。關係似乎是按順序的,控制器也是如此。欄目嵌套屬性不顯示在表格中

這是代碼。

控制器

def new 
    @profile = current_user.build_student_profile 
end 

def profile_params 
    params.require(:student_profile).permit(:first_name, :last_name, :gender, student_profiles_attributes: [:degree, :university_id, :major, :major2, :start_date, :end_date, :grade, :grade_scale]) 
end 

模式

class Education < ActiveRecord::Base 
    belongs_to :student_profile 
    belongs_to :university 
    validates :grade_scale, inclusion: { in: %w(GPA4 GPA7 WAM100) } 
    validates :degree, :university_id, :major, :start_date, :end_date, :grade, :grade_scale, presence: true 
end 
class StudentProfile < ActiveRecord::Base 
    belongs_to :user 
    has_many :educations 
    validates :gender, inclusion: { in: %w(male female) } 
    validates :first_name, :last_name, :gender, presence: true 
    accepts_nested_attributes_for :educations 
end 

<%= form_for (@profile) do |f| %> 
    <%= f.label :first_name %> 
    <%= f.text_field :first_name %> 
    <%= f.label :last_name %> 
    <%= f.text_field :last_name %> 
    <%= f.label :gender %> 
    <%= f.text_field :gender %> 
    <%= f.fields_for :educations do |education_fields| %> 
    <%= education_fields.label :Degree %> 
    <%= education_fields.text_field :degree %> 
    <%= education_fields.label :University %> 
    <%= education_fields.collection_select(:university_id, University.all, :id, :name) %> 
    <%= education_fields.label :Major %> 
    <%= education_fields.text_field :major %> 
    <%= education_fields.label :Additional_Major %> 
    <%= education_fields.text_field :major2 %> 
    <%= education_fields.label :Start_Date %> 
    <%= education_fields.date_field :start_date %> 
    <%= education_fields.label :End_Date %> 
    <%= education_fields.date_field :end_date %> 
    <%= education_fields.label :Grade %> 
    <%= education_fields.number_field :grade %> 
    <%= education_fields.label :Grade_Scale %> 
    <%= education_fields.select :grade_scale, [["GPA/4","GPA4"], ["GPA/7","GPA7"], ["WAM/100","WAM100"]] %> 
    <% end %> 
    <%= f.submit :submit %> 
<% end %> 

我試圖添加弗洛翼控制器新行動@profile.educations.build但我得到一個錯誤未知屬性student_profile_id

任何人都可以幫忙嗎?

回答

0

確保您有student_profile_id屬性/列出現在educations表中。

之後,正如你所提到的,你需要建立educations對象student_profile爲:

def new 
    @profile = current_user.build_student_profile 
    @profile.educations.build 
end 
+0

我試過了,我得到了'ActiveRecord :: UnknownAttributeError student_profile_id' – Eali

+0

確保你有'student_profile_id'屬性/列出現在教育表中。 – RAJ

+0

謝謝你,它看起來像我把id放在錯誤的表 – Eali

0

試試這個

<%= f.fields_for(:educations,@profile.educations.build) do |education_fields| %> <% end %>

def new @profile = current_user.build_student_profile @educations = @profile.educations.build end

<%= f.fields_for(@educations) do |education_fields| %> <% end %>

+0

我沒有在educations表中的student_profile_id – Eali

+0

運行遷移並在表中添加student_profile_id .. – Ravindra