【问题标题】:Rails - HABTM find or create nested formRails - HABTM 查找或创建嵌套表单
【发布时间】:2015-06-09 04:04:05
【问题描述】:

我找到了this。

我现在的问题是如何创建表单,我正在尝试:

        #students.row
          = f.fields_for :classrooms_students, f.object.classrooms_students do |cs|
            = cs.fields_for :student, cs.object.student do |s|
              / .record is used to fill fields when teacher clicks a .result
              .record
                .col-sm-6 style=("margin-top: 10px;")
                  = s.label :name
                  = s.text_field :name, class: 'form-control autocomplete', data: {autocomplete: :name}
                  .results
                .col-sm-6 style=("margin-top: 10px;")
                  = s.label :email
                  = s.text_field :email, class: 'form-control autocomplete', data: {autocomplete: :email}
                  .results
            .col-sm-12 style=("margin-top: 10px;")
              = cs.link_to_remove 'Remove Student', class: 'btn btn-danger'
          .col-sm-12 style=("margin-top: 10px;")
            = f.link_to_add 'Add Student', :classrooms_students, "data-target" => '#students', class: 'btn btn-primary'

问题是我在服务器中得到:

{"classroom"=>
  {"name"=>"qwerqqeq",
   "description"=>"",
   "note"=>"",
   "classrooms_students_attributes"=>
    {"0"=>
      {"student_attributes"=>{"name"=>"vsrjh", "email"=>"fgh@fgh.com", "id"=>"33"},
       "_destroy"=>"false",
       "id"=>""},
     "1"=>
      {"student_attributes"=>{"name"=>"cvbcbc", "email"=>"yui@yui.com", "id"=>"32"},
       "_destroy"=>"false",
       "id"=>""}}}}

连接哈希上的id 是'' Rails 将如何查找记录?

所以我得到:

undefined method `to_sym' for nil:NilClass.

我应该添加一个id 列来连接表吗?

【问题讨论】:

    标签: ruby-on-rails nested-forms has-and-belongs-to-many nested-form-for


    【解决方案1】:

    我添加了index:

        add_column :classrooms_students, :id, :primary_key
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-01
      • 1970-01-01
      相关资源
      最近更新 更多