Rails:创建新记录的许多对多个问题

时间:2017-10-15 03:56:55

标签: ruby-on-rails ruby-on-rails-4 many-to-many nested-forms has-many-through

我有一个我一直在研究的rails应用程序,它有三个多对多的型号。基本上,该应用程序的课程可以包含许多位置和位置,可以通过courseassociations拥有许多教师。

当用户创建新课程时,我希望他们添加课程详细信息,添加新位置或现有位置,并为每个位置分配或创建新教师。

我的应用目前看起来如下;

模型

class Course < ActiveRecord::Base
  has_many :courseassociations, :dependent => :destroy
  has_many :courseinstructors, through: :courseassociations
  has_many :courselocations, through: :courseassociations

  accepts_nested_attributes_for :courselocations
  accepts_nested_attributes_for :courseinstructors
end

class Courselocation < ActiveRecord::Base
  has_many :courseassociations
  has_many :courseinstructors, through: :courseassociations
  has_many :courses, through: :courseassociations
end

class Courseinstructor < ActiveRecord::Base
  has_many :courseassocaitions
  has_many :courses, through: :courseassocaitions
end

控制器

class Admin::CoursesController < ApplicationController
  def new
    @course = Course.new
    1.times {@course.courselocations.build}
    1.times {@course.courseinstructors.build}
  end
    def course_params
      params.require(:course).permit(:name, :summary, :description, :signup_link, :price, courselocations_attributes: [:id, :name, :address, :city, :state, :zipcode, :_destroy], courseinstructors_attributes: [:first_name, :last_name, :title, :company, :image, :_destroy, :id])

    end
end

查看(通过课程嵌套表单)

  <% if @course.new_record? %>
    <%= f.fields_for :courselocations do |builder| %>
      <%= render 'courselocation_fields', f: builder %>
      <%= f.fields_for :courseinstructors do |builder| %>
        <%= render 'courseinstructor_fields', f: builder %>
      <% end %>
    <% end %>
  <% end %>

在我的控制器中,让courselocations_attributes: [...]courseinstructors_attributes: [...]返回Unpermitted parameter: courseinstructors_attributes

如果我这样改变控制器; courselocations_attributes: [..., courseinstructors_attributes: [...]]这可行,但我的courseassociations表如下所示;

| id | course_id | courselocation_id | courseinstructor_id | created_at          | updated_at          |
+----+-----------+-------------------+---------------------+---------------------+---------------------+
|  1 |         1 |                 1 |                NULL | 2017-10-13 03:15:43 | 2017-10-13 03:15:43 |
|  2 |         2 |                 2 |                NULL | 2017-10-13 03:35:05 | 2017-10-13 03:35:05 |
|  3 |         2 |                 3 |                NULL | 2017-10-13 03:35:05 | 2017-10-13 03:35:05 |
|  4 |      NULL |                 3 |                   1 | 2017-10-13 15:33:46 | 2017-10-13 03:35:05

我期待的是

| id | course_id | courselocation_id | courseinstructor_id | created_at          | updated_at          |
+----+-----------+-------------------+---------------------+---------------------+---------------------+
|  1 |         1 |                 1 |                NULL | 2017-10-13 03:15:43 | 2017-10-13 03:15:43 |
|  2 |         2 |                 2 |                NULL | 2017-10-13 03:35:05 | 2017-10-13 03:35:05 |
|  3 |         2 |                 3 |                   1 | 2017-10-13 03:35:05 | 2017-10-13 03:35:05 |
|  4 |         2 |                 3 |                   2 | 2017-10-13 03:35:05 | 2017-10-13 03:35:05 |

不确定我在这里做错了什么?

示例控制台输出

Started POST "/admin/courses" for ::1 at 2017-10-14 23:02:48 -0400
Processing by Admin::CoursesController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"aXe/nmTm32mBoA50eXea8b45CXheQh22fCIqcX6mzmHqJTDBD/pbTe5ZKUavyMCyJumOVJwI5KPAKb/6J2C3dQ==", "commit"=>"Save", "course"=>{"name"=>"Test IBE Course", "summary"=>"Test IBE Course", "description"=>"<p>\r\n\tTest IBE Course\r\n</p>", "price"=>"1000.00", "signup_link"=>"Test", "courselocations_attributes"=>{"0"=>{"name"=>"IBE 1", "address"=>"Some road", "city"=>"Boston", "state"=>"MA", "zipcode"=>"02135", "_destroy"=>"false", "courseinstructors_attributes"=>{"1508036554699"=>{"first_name"=>"Kyla", "last_name"=>"Slen", "title"=>"VP", "company"=>"EurekaConnect", "image"=>#<ActionDispatch::Http::UploadedFile:0x007faf7e27c0d8 @tempfile=#<Tempfile:/var/folders/87/27b7nz496w1dms5_my1z2mxm0000gn/T/RackMultipart20171014-43380-10zghle.jpg>, @original_filename="250px-PlagueOfInsecticons_Bumblebee_and_Spike_hide.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"course[courselocations_attributes][0][courseinstructors_attributes][1508036554699][image]\"; filename=\"250px-PlagueOfInsecticons_Bumblebee_and_Spike_hide.jpg\"\r\nContent-Type: image/jpeg\r\n">, "_destroy"=>"false"}}}}}}
  User Load (0.3ms)  SELECT  `users`.* FROM `users` WHERE `users`.`id` = 1  ORDER BY `users`.`id` ASC LIMIT 1
   (0.2ms)  BEGIN
   (0.2ms)  SELECT COUNT(*) FROM `courses` WHERE (permalink = 'test-ibe-course')
   (0.2ms)  SELECT COUNT(*) FROM `courselocations` WHERE (permalink = 'ibe-1-boston-ma')
  Courselocation Load (23.8ms)  SELECT `courselocations`.* FROM `courselocations` WHERE (permalink LIKE 'ibe-1-boston-ma%')  ORDER BY id
   (0.4ms)  SELECT COUNT(*) FROM `courseinstructors` WHERE (permalink = 'kyla-slen')
  SQL (0.3ms)  INSERT INTO `courses` (`name`, `summary`, `description`, `signup_link`, `price`, `permalink`, `created_at`, `updated_at`) VALUES ('Test IBE Course', 'Test IBE Course', '<p>\r\n Test IBE Course\r\n</p>', 'Test', '1000.00', 'test-ibe-course', '2017-10-15 03:02:49', '2017-10-15 03:02:49')
  SQL (0.2ms)  INSERT INTO `courselocations` (`name`, `address`, `city`, `state`, `zipcode`, `permalink`, `created_at`, `updated_at`) VALUES ('IBE 1', 'Some road', 'Boston', 'MA', '02135', 'ibe-1-boston-ma-1', '2017-10-15 03:02:49', '2017-10-15 03:02:49')
  SQL (0.3ms)  INSERT INTO `courseinstructors` (`first_name`, `last_name`, `title`, `company`, `image`, `permalink`, `created_at`, `updated_at`) VALUES ('Kyla', 'Slen', 'VP', 'EurekaConnect', 'ibeinc/bc2uq6g1ttzeycbge0gc.jpg', 'kyla-slen', '2017-10-15 03:02:49', '2017-10-15 03:02:49')
  SQL (0.5ms)  UPDATE `courseinstructors` SET `courseinstructors`.`image` = 'image/upload/v1508036568/ibeinc/bc2uq6g1ttzeycbge0gc.jpg' WHERE `courseinstructors`.`id` = 2
  SQL (0.3ms)  UPDATE `courselocations` SET `name` = 'IBE 1', `address` = 'Some road', `city` = 'Boston', `state` = 'MA', `zipcode` = '02135', `permalink` = 'ibe-1-boston-ma-1', `created_at` = '2017-10-15 03:02:49', `updated_at` = '2017-10-15 03:02:49', `id` = 8 WHERE `courselocations`.`id` = 8
  SQL (0.2ms)  INSERT INTO `courseassociations` (`courselocation_id`, `courseinstructor_id`, `created_at`, `updated_at`) VALUES (8, 2, '2017-10-15 03:02:50', '2017-10-15 03:02:50')
  SQL (0.2ms)  UPDATE `courses` SET `name` = 'Test IBE Course', `summary` = 'Test IBE Course', `description` = '<p>\r\n Test IBE Course\r\n</p>', `signup_link` = 'Test', `price` = '1000.00', `permalink` = 'test-ibe-course', `created_at` = '2017-10-15 03:02:49', `updated_at` = '2017-10-15 03:02:49', `id` = 7 WHERE `courses`.`id` = 7
  SQL (0.1ms)  INSERT INTO `courseassociations` (`course_id`, `courselocation_id`, `created_at`, `updated_at`) VALUES (7, 8, '2017-10-15 03:02:50', '2017-10-15 03:02:50')
   (6.9ms)  COMMIT

0 个答案:

没有答案