Rails 4嵌套表单在编辑中显示UP,但不在模态

时间:2016-08-15 10:13:28

标签: ruby-on-rails forms bootstrap-modal

我有一个用于编辑屏幕的表单,我也在另一个屏幕上填充Modal(当用户刚刚注册时)。但是,仅在该Modal上的嵌套表单显示有一点错误。我一直试图找到它几个小时。这是我的代码:

= form_for @user do |user|

.FORM组     %label {:for => :名字}       名字     = user.text_field:first_name,class:“form-control”

%label{:for => :middle_name}
  Middle Name
= user.text_field :middle_name, class: "form-control"

%label{:for => :last_name}
  Last Name
= user.text_field :last_name, class: "form-control"

%label{:for => :image}
  Profile Picture
= image_tag @user.image.url(:thumb),  class: "img-circle" if @user.image.exists?
= user.file_field :image, class: "form-control"

%label{:for => :headline}
  Headline
= user.text_field :headline, class: "form-control"

%label{:for => :bio}
  What are you currently Involved with? (Hint Just copy your LinkedIn Highlight)
= user.text_area :bio, class: "form-control user_text_area"

%label{:for => :twitter}
  Your Twitter Handle
= user.text_field :twitter, class: "form-control"


%label{:for => :school}
  School
= collection_select(:user, :school_id, School.all, :id, :name,  {prompt: true, :required => ""}, {  class: "filter-option pull-left form-control" } )

%br/

%label{:for => :course}
Course
= user.fields_for :course_user do |cu|
  = cu.select :course_id, Course.all.map { |c| [c.name_with_intake, c.id] }, {}, class: 'filter-option pull-left form-control', id: 'courses_select'
-# = collection_select(:course, :course_id, Course.all, :id, :name, {:prompt   => "Select a Course"}, {:id => 'courses_select', class: "filter-option pull-left form-control"})

然后我的用户模态确实显示了这个:

has_one :course_user
accepts_nested_attributes_for :course_user

我坚持使用的字段是向用户添加课程的“课程字段”。现在,这是其他人已建议删除用户部分,因此代码将是:

%label{:for => :course}
Course
= fields_for :course_user do |cu|
  = cu.select :course_id, Course.all.map { |c| [c.name_with_intake, c.id] }, 

或者将:course_user更改为CourseUser.new

%label{:for => :course}
Course
= fields_for CourseUser.new do |cu|
  = cu.select :course_id, Course.all.map { |c| [c.name_with_intake, c.id] }, 

两者都会导致在模态中显示该字段。但是,两者都会阻止数据被保存。所以我想知道......出了什么问题?我很感激任何帮助!

1 个答案:

答案 0 :(得分:0)

经过一个漫长的夜晚想出来并最终打电话给帮助热线,我发现了问题所在。用户控制器编辑有一条我完全忽略的线路使它工作。所以你需要有user.fields_for。如果需要,我可以帮助您解决类似的问题。

= user.fields_for :course_user do |cu|

这是最终有效的代码。我有一些步骤要做:

1 - 查看服务器日志,查看解析Params的内容。检查参数

2 - 再次在服务器日志中,查看推送的项目

3 - 然后检查Controller中的操作以及是否添加了

accepts_nested_attributes_for :course_user

到相应的模型(更改:course_user)