Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 4 在创建子对象之前捕获父对象信息_Ruby On Rails 4_Nested Forms_Friendly Id_Before Save - Fatal编程技术网

Ruby on rails 4 在创建子对象之前捕获父对象信息

Ruby on rails 4 在创建子对象之前捕获父对象信息,ruby-on-rails-4,nested-forms,friendly-id,before-save,Ruby On Rails 4,Nested Forms,Friendly Id,Before Save,我正在使用两种型号:地点和产品 地点是父项,产品是子项。我以前让用户分别创建这些记录,首先让用户创建位置,然后再创建产品 我现在使用嵌套的表单来减少额外的步骤 我现在遇到的问题是格式化的URL创建(使用友好id) 之前,正如您在下面看到的,我的url是由从其父级收集的元素创建的 我现在遇到的问题是,由于父对象与子对象同时创建,因此在保存位置之前,它无法分配该位置的当前位置并将其发布给子对象。作为回报,这不允许我的产品拾取我以前用于生成url的位置字段 是否有一种方法可以让我收集所有位置信息,在创

我正在使用两种型号:地点和产品

地点是父项,产品是子项。我以前让用户分别创建这些记录,首先让用户创建位置,然后再创建产品

我现在使用嵌套的表单来减少额外的步骤

我现在遇到的问题是格式化的URL创建(使用友好id)

之前,正如您在下面看到的,我的url是由从其父级收集的元素创建的

我现在遇到的问题是,由于父对象与子对象同时创建,因此在保存位置之前,它无法分配该位置的当前位置并将其发布给子对象。作为回报,这不允许我的产品拾取我以前用于生成url的位置字段

是否有一种方法可以让我收集所有位置信息,在创建产品之前将其传递给产品,以便它能够生成我需要的url字符串

位置模型

class Location < ActiveRecord::Base

 has_many :products, dependent: :destroy
 accepts_nested_attributes_for :products, :allow_destroy => :true
 validates_associated :products

end
class Product < ActiveRecord::Base

 extend FriendlyId
 friendly_id :slug_candidates, use: :slugged

 def slug_candidates
  [
    [location.media_type, location.state, location.name, :name, :sku]
  ]
 end
日志

Started POST "/locations" for 127.0.0.1 at 2014-05-16 08:51:01 -0700
Processing by LocationsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"vD4e68ZoJUD0Qp/XUS1Co4nzqyZ6Zf6fshdFL2/7TXA=",  "location"=>{"name"=>"Testing", "vendor_id"=>"1", "media_type"=>"Digital Billboards", "market"=>"SF   Bay Area", "city"=>"santa clara", "state"=>"CA", "zipcode"=>"95124", "longitude"=>"", "latitude"=>"", "installation_side"=>"", "ad_rotation_frequency"=>"", "ad_spot_duration"=>"", "weekly_cycle"=>"", "board_width"=>"", "board_height"=>"", "ad_size_width"=>"", "ad_size_height"=>"", "supported_ad_types"=>"", "age_demographic"=>"", "ethnicity_demo_white"=>"", "ethnicity_demo_black"=>"", "ethnicity_demo_asian"=>"", "ethnicity_demo_hispanic"=>"", "products_attributes"=>{"0"=>{"name"=>"North Face", "sku"=>"1001", "tab_id"=>"", "available_spots"=>"", "dec"=>"", "ooh"=>"", "retail_weekly_price"=>"", "reserve_price"=>"", "description"=>"", "_destroy"=>"false"}}}, "commit"=>"Create Location"}
  User Load (0.3ms)  SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 ORDER BY `users`.`id` ASC LIMIT 1
  (0.1ms)  BEGIN
  Location Exists (0.3ms)  SELECT 1 AS one FROM `locations` WHERE `locations`.`slug` = 'testing' LIMIT 1
  Product Exists (0.3ms)  SELECT 1 AS one FROM `products` WHERE `products`.`slug` = 'north-face-1001' LIMIT 1
  Product Exists (0.3ms)  SELECT 1 AS one FROM `products` WHERE `products`.`slug` = 'north-face-1001' AND (`products`.`id` IS NOT NULL) LIMIT 1
  SQL (0.3ms)  INSERT INTO `locations` (`ad_rotation_frequency`, `age_demographic`, `city`, `created_at`, `installation_side`, `market`, `media_type`, `name`, `slug`, `state`, `supported_ad_types`, `updated_at`, `vendor_id`, `weekly_cycle`, `zipcode`) VALUES ('', '', 'santa clara', '2014-05-16 15:51:01', '', 'SF Bay Area', 'Digital Billboards', 'Testing', 'testing', 'CA', '', '2014-05-16 15:51:01', 1, '', 95124)
  Location Load (0.3ms)  SELECT `locations`.* FROM `locations` WHERE `locations`.`id` = 119 ORDER BY `locations`.`id` ASC LIMIT 1
  Vendor Load (0.2ms)  SELECT `vendors`.* FROM `vendors` WHERE `vendors`.`id` = 1 ORDER BY `vendors`.`id` ASC LIMIT 1
  SQL (30.0ms)  INSERT INTO `products` (`created_at`, `description`, `location_id`, `location_name`, `market`, `name`, `sku`, `slug`, `tab_id`, `updated_at`, `vendor_id`, `vendor_name`) VALUES ('2014-05-16 15:51:01', '', 119, 'Testing', 'SF Bay Area', 'North Face', '1001', 'north-face-1001', '', '2014-05-16 15:51:01', 1, 'AdSemble')
   (11.2ms)  COMMIT
   Redirected to http://localhost:3000/locations/testing
   Completed 302 Found in 63ms (ActiveRecord: 43.4ms)

你是如何通过这些参数的?你允许他们吗?是的。它们在my locations controller:params.require(:location.permit(..,products_attributes:[:id,:name,…])中传递。您可以共享日志吗?视图和产品_attributes中,您为什么传递id?@Babar:对不起,您可以重新表述您的问题吗?产品_属性[:id]用于嵌套的_表单,因此它只创建一条记录
Started POST "/locations" for 127.0.0.1 at 2014-05-16 08:51:01 -0700
Processing by LocationsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"vD4e68ZoJUD0Qp/XUS1Co4nzqyZ6Zf6fshdFL2/7TXA=",  "location"=>{"name"=>"Testing", "vendor_id"=>"1", "media_type"=>"Digital Billboards", "market"=>"SF   Bay Area", "city"=>"santa clara", "state"=>"CA", "zipcode"=>"95124", "longitude"=>"", "latitude"=>"", "installation_side"=>"", "ad_rotation_frequency"=>"", "ad_spot_duration"=>"", "weekly_cycle"=>"", "board_width"=>"", "board_height"=>"", "ad_size_width"=>"", "ad_size_height"=>"", "supported_ad_types"=>"", "age_demographic"=>"", "ethnicity_demo_white"=>"", "ethnicity_demo_black"=>"", "ethnicity_demo_asian"=>"", "ethnicity_demo_hispanic"=>"", "products_attributes"=>{"0"=>{"name"=>"North Face", "sku"=>"1001", "tab_id"=>"", "available_spots"=>"", "dec"=>"", "ooh"=>"", "retail_weekly_price"=>"", "reserve_price"=>"", "description"=>"", "_destroy"=>"false"}}}, "commit"=>"Create Location"}
  User Load (0.3ms)  SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 ORDER BY `users`.`id` ASC LIMIT 1
  (0.1ms)  BEGIN
  Location Exists (0.3ms)  SELECT 1 AS one FROM `locations` WHERE `locations`.`slug` = 'testing' LIMIT 1
  Product Exists (0.3ms)  SELECT 1 AS one FROM `products` WHERE `products`.`slug` = 'north-face-1001' LIMIT 1
  Product Exists (0.3ms)  SELECT 1 AS one FROM `products` WHERE `products`.`slug` = 'north-face-1001' AND (`products`.`id` IS NOT NULL) LIMIT 1
  SQL (0.3ms)  INSERT INTO `locations` (`ad_rotation_frequency`, `age_demographic`, `city`, `created_at`, `installation_side`, `market`, `media_type`, `name`, `slug`, `state`, `supported_ad_types`, `updated_at`, `vendor_id`, `weekly_cycle`, `zipcode`) VALUES ('', '', 'santa clara', '2014-05-16 15:51:01', '', 'SF Bay Area', 'Digital Billboards', 'Testing', 'testing', 'CA', '', '2014-05-16 15:51:01', 1, '', 95124)
  Location Load (0.3ms)  SELECT `locations`.* FROM `locations` WHERE `locations`.`id` = 119 ORDER BY `locations`.`id` ASC LIMIT 1
  Vendor Load (0.2ms)  SELECT `vendors`.* FROM `vendors` WHERE `vendors`.`id` = 1 ORDER BY `vendors`.`id` ASC LIMIT 1
  SQL (30.0ms)  INSERT INTO `products` (`created_at`, `description`, `location_id`, `location_name`, `market`, `name`, `sku`, `slug`, `tab_id`, `updated_at`, `vendor_id`, `vendor_name`) VALUES ('2014-05-16 15:51:01', '', 119, 'Testing', 'SF Bay Area', 'North Face', '1001', 'north-face-1001', '', '2014-05-16 15:51:01', 1, 'AdSemble')
   (11.2ms)  COMMIT
   Redirected to http://localhost:3000/locations/testing
   Completed 302 Found in 63ms (ActiveRecord: 43.4ms)