首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >rails嵌套属性不以形式显示。

rails嵌套属性不以形式显示。
EN

Stack Overflow用户
提问于 2015-05-18 10:40:13
回答 2查看 145关注 0票数 0

我在表单中添加了一个嵌套属性,嵌套属性的字段--即Education字段不呈现,其他字段则呈现。这些关系似乎是有序的,而控制器也是如此。

这是密码。

控制器

代码语言:javascript
复制
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

模型

代码语言:javascript
复制
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

表单

代码语言:javascript
复制
<%= 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

有人能帮忙吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-05-18 10:43:49

确保student_profile_id属性/列存在于educations表中。

在此之后,正如您已经提到的,您需要在educations上构建student_profile对象,如下所示:

代码语言:javascript
复制
def new
  @profile = current_user.build_student_profile
  @profile.educations.build
end
票数 0
EN

Stack Overflow用户

发布于 2015-05-18 11:04:37

尝尝这个

<%= 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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30301026

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档