我使用的是Rails 3.2.18。
我有Report Template
模型,其中包含许多Report Template
元素。使用 fields_for 我正在构建表单以更新特定报告模板的所有Report Template
元素。正在发送正确的参数(形成我所理解的),但数据库中的记录不会更新。
报告模板:
class ReportTemplate < ActiveRecord::Base
has_many :report_template_elements
accepts_nested_attributes_for :report_template_elements
attr_accessible :report_template_elements_attributes
end
报告模板控制器:
class ReportTemplatesController < ApplicationController
def update
@report_template.active_user = current_user
@report_template.update_attributes params[:report_template]
respond_to do |format|
format.html { redirect_to @report_template }
format.js { render :nothing => true }
end
end
end
我的表格有点复杂(各种部分等),但是我使用这种模式:
= form_for @report_template, html: { id: 'report-template-form' } do |form|
#in partials I am iterating over report_template_elements, objects stored in 'element' variable
= form.fields_for :report_template_elements, element do |f|
#element fields here
表单提交时发送的参数如下(对于两个报告模板元素):
Parameters: {"utf8"=>"✓", "authenticity_token"=>"ywY1Zqde9uyoDpWpSyfqjWWuGbfveaR1cf+S9WUGpUM=", "report_template"=>{"report_template_elements_attributes"=>{"0"=>{"text_align"=>"center", "color"=>"#6bfa6e", "font_family"=>"Andale Mono", "font_size"=>"larger", "font_weight"=>"normal", "font_style"=>"italic", "text_decoration"=>"underline", "id"=>"816"}, "1"=>{"text_align"=>"center", "color"=>"#000000", "font_family"=>"Arial", "font_size"=>"smaller", "font_weight"=>"normal", "font_style"=>"normal", "text_decoration"=>"none", "id"=>"817"}}}, "id"=>"___570490880"}
没有错误,但更改未保存到数据库(日志中没有UPDATE)。在我添加Can't mass-assign protected attributes: report_template_elements_attributes
之前,我曾经获得attr_accessible :report_template_elements_attributes
。
我不知道我做错了什么,我会感激任何帮助。
编辑:
整个请求日志:
[2014/07/14 10:36:06] (INFO) 965 Started PUT "/report_templates/___570490880" for 127.0.0.1 at 2014-07-14 10:36:06 +0200
[2014/07/14 10:36:06] (INFO) 965 Processing by ReportTemplatesController#update as HTML
[2014/07/14 10:36:06] (INFO) 965 Parameters: {"utf8"=>"✓", "authenticity_token"=>"ywY1Zqde9uyoDpWpSyfqjWWuGbfveaR1cf+S9WUGpUM=", "report_template"=>{"report_template_elements_attributes"=>{"0"=>{"text_align"=>"center", "color"=>"#cf1270", "font_family"=>"Tahoma", "font_size"=>"larger", "font_weight"=>"bold", "font_style"=>"normal", "text_decoration"=>"none", "id"=>"816"}, "1"=>{"text_align"=>"center", "color"=>"#000000", "font_family"=>"Arial", "font_size"=>"smaller", "font_weight"=>"normal", "font_style"=>"normal", "text_decoration"=>"none", "id"=>"817"}, "2"=>{"text_align"=>"left", "color"=>"#000000", "font_family"=>"Arial", "font_size"=>"normal", "font_weight"=>"bold", "font_style"=>"normal", "text_decoration"=>"none", "id"=>"819"}, "3"=>{"text_align"=>"left", "color"=>"#000000", "font_family"=>"Arial", "font_size"=>"normal", "font_weight"=>"bold", "font_style"=>"normal", "text_decoration"=>"none", "id"=>"820"}, "4"=>{"text_align"=>"left", "color"=>"#000000", "font_family"=>"Arial", "font_size"=>"normal", "font_weight"=>"bold", "font_style"=>"normal", "text_decoration"=>"none", "id"=>"821"}, "5"=>{"text_align"=>"left", "color"=>"#000000", "font_family"=>"Arial", "font_size"=>"smaller", "font_weight"=>"normal", "font_style"=>"normal", "text_decoration"=>"none", "id"=>"822"}, "6"=>{"text_align"=>"left", "color"=>"#000000", "font_family"=>"Arial", "font_size"=>"smaller", "font_weight"=>"normal", "font_style"=>"normal", "text_decoration"=>"none", "id"=>"823"}, "7"=>{"text_align"=>"right", "color"=>"#000000", "font_family"=>"Arial", "font_size"=>"smaller", "font_weight"=>"normal", "font_style"=>"normal", "text_decoration"=>"none", "id"=>"1657"}}}, "text-or-image_Center"=>["text"], "textValue_Center"=>"Center", "text-or-image_Right"=>["text"], "textValue_Right"=>"Right", "id"=>"___570490880"}
[2014/07/14 10:36:06] (DEBUG) 965 User Load (0.4ms) SELECT `general_users`.* FROM `general_users` WHERE `general_users`.`type` IN ('User') AND `general_users`.`id` = 1 AND (general_users.delete_status != 0) LIMIT 1
[2014/07/14 10:36:06] (DEBUG) 965 CACHE (0.0ms) SELECT `general_users`.* FROM `general_users` WHERE `general_users`.`type` IN ('User') AND `general_users`.`id` = 1 AND (general_users.delete_status != 0) LIMIT 1
[2014/07/14 10:36:06] (DEBUG) 965 UserSetting Load (0.2ms) SELECT `user_settings`.* FROM `user_settings` WHERE `user_settings`.`user_id` = 1 LIMIT 1
[2014/07/14 10:36:06] (DEBUG) 965 LanguageCode Load (0.2ms) SELECT `language_codes`.* FROM `language_codes` WHERE `language_codes`.`id` = 16 ORDER BY name LIMIT 1
[2014/07/14 10:36:06] (DEBUG) 965 LanguageCode Load (0.9ms) SELECT `language_codes`.* FROM `language_codes` ORDER BY name
[2014/07/14 10:36:06] (DEBUG) 965 SQL (0.6ms) SELECT `usage_rights`.`resourceable_id` FROM `usage_rights` WHERE `usage_rights`.`user_id` = 1 AND `usage_rights`.`resourceable_type` = 'ReportTemplate' AND `usage_rights`.`active` = 1
[2014/07/14 10:36:06] (DEBUG) 965 ReportTemplate Load (0.6ms) SELECT `report_templates`.* FROM `report_templates` WHERE (report_templates.delete_status != 0) AND ((report_templates.owner_id = 1) OR (report_templates.id IN (NULL))) ORDER BY name
[2014/07/14 10:36:06] (DEBUG) 965 SQL (0.3ms) SELECT `usage_rights`.`resourceable_id` FROM `usage_rights` WHERE `usage_rights`.`user_id` = 1 AND `usage_rights`.`resourceable_type` = 'ReportTemplate' AND `usage_rights`.`active` = 1
[2014/07/14 10:36:06] (DEBUG) 965 ReportTemplate Load (0.5ms) SELECT `report_templates`.* FROM `report_templates` WHERE ((report_templates.owner_id = 1) OR (report_templates.id IN (NULL))) ORDER BY name
[2014/07/14 10:36:06] (DEBUG) 965 ReportTemplate Load (0.4ms) SELECT `report_templates`.* FROM `report_templates` WHERE `report_templates`.`id` = 35 AND (report_templates.delete_status != 0) LIMIT 1
[2014/07/14 10:36:06] (DEBUG) 965 CACHE (0.0ms) SELECT `report_templates`.* FROM `report_templates` WHERE `report_templates`.`id` = 35 AND (report_templates.delete_status != 0) LIMIT 1
[2014/07/14 10:36:06] (DEBUG) 965 (0.1ms) BEGIN
[2014/07/14 10:36:06] (DEBUG) 965 ReportTemplateElement Load (0.4ms) SELECT `report_template_elements`.* FROM `report_template_elements` WHERE `report_template_elements`.`report_template_id` = 35 AND `report_template_elements`.`id` IN (816, 817, 819, 820, 821, 822, 823, 1657)
[2014/07/14 10:36:06] (DEBUG) 965 CACHE (0.0ms) SELECT `general_users`.* FROM `general_users` WHERE `general_users`.`type` IN ('User') AND `general_users`.`id` = 1 AND (general_users.delete_status != 0) LIMIT 1
[2014/07/14 10:36:06] (DEBUG) 965 (0.7ms) SELECT COUNT(*) FROM `report_templates` INNER JOIN `general_users` ON `general_users`.`id` = `report_templates`.`owner_id` AND `general_users`.`type` IN ('User') WHERE (report_templates.name = 'Extended DISC Template (Silver)') AND (report_templates.id <> 35) AND ((general_users.role_id = 1))
[2014/07/14 10:36:06] (DEBUG) 965 GeneralUser Load (0.5ms) SELECT `general_users`.* FROM `general_users` WHERE `general_users`.`role_id` = 2 AND (`general_users`.`lft` <= 63 AND `general_users`.`rgt` >= 830) AND (general_users.delete_status != 0) ORDER BY `general_users`.`lft` LIMIT 1
[2014/07/14 10:36:06] (DEBUG) 965 SQL (0.7ms) SELECT `general_users`.`id` FROM `general_users` WHERE (`general_users`.`lft` >= 63 AND `general_users`.`lft` < 830) AND (general_users.delete_status != 0) ORDER BY `general_users`.`lft`
[2014/07/14 10:36:06] (DEBUG) 965 UsageRight Load (0.9ms) SELECT `usage_rights`.* FROM `usage_rights` WHERE `usage_rights`.`resourceable_id` = 35 AND `usage_rights`.`resourceable_type` = 'ReportTemplate' AND (user_id IN (1,8,48,140,252,267,337,382,556,608,609,610,660,863,779,859,29,50,51,52,53,54,682,55,246,56,57,723,58,260,59,67,123,170,270,243,383,269,731,286,300,516,567,728,830,833,834,835,836,837,838,839,831,832,573,575,576,577,578,579,647,689,744,765,757,764,816,841,861,864,865,866,867,868,871,873,875,879,60,61,62,63,64,65,66,75,148,78,366,372,373,376,377,380,391,392,396,397,398,399,400,443,733,734,756,87,147,160,184,386,387,388,685,843,844,870,88,108,346,109,110,348,271,347,273,342,349,368,381,409,425,426,427,428,429,430,431,435,445,505,615,643,650,89,106,151,338,340,341,351,353,393,560,90,101,129,130,163,105,122,127,262,517,518,562,563,564,565,91,557,854,872,92,555,587,98,112,114,120,172,362,279,281,336,506,813,99,100,314,315,316,317,318,319,107,117,118,119,274,275,355,363,364,365,403,417,424,432,433,434,444,453,504,152,157,158,166,167,171,161,162,174,175,176,192,177,305,323,324,325,326,311,327,328,329,330,331,332,333,334,335,410,411,438,412,441,442,413,436,437,440,414,415,416,439,452,507,508,509,420,421,422,423,446,448,449,450,451,510,511,537,512,538,513,539,540,514,542,515,541,521,522,523,524,525,183,259,354,374,378,856,195,606,607,637,649,659,667,845,651,652,853,869,198,199,200,201,202,203,204,207,208,216,210,217,221,232,234,248,249,250,251,209,218,771,211,229,212,215,213,308,343,222,633,230,231,233,302,306,307,255,258,261,264,265,395,266,384,385,394,566,568,824,825,826,827,626,627,738,815,829,862,874,876,877,878))
[2014/07/14 10:36:06] (DEBUG) 965 SQL (0.3ms) SELECT `usage_rights`.id FROM `usage_rights` WHERE `usage_rights`.`resourceable_id` = 35 AND `usage_rights`.`resourceable_type` = 'ReportTemplate'
[2014/07/14 10:36:06] (DEBUG) 965 SQL (0.8ms) SELECT `general_users`.`id` FROM `general_users` WHERE (`general_users`.`lft` >= 63 AND `general_users`.`lft` < 830) AND (general_users.delete_status != 0) ORDER BY `general_users`.`lft`
[2014/07/14 10:36:06] (DEBUG) 965 SQL (0.3ms) INSERT INTO `activities` (`activator_id`, `activator_type`, `activity_type`, `created_at`, `label`, `updated_at`, `user_id`) VALUES (35, 'ReportTemplate', 2, '2014-07-14 08:36:06', 'Extended DISC Template (Silver)', '2014-07-14 08:36:06', 1)
[2014/07/14 10:36:06] (DEBUG) 965 (0.6ms) COMMIT
根据Mike Campbell的建议,我在ReportTemplatesController中绑定update_attributes!
并且没有任何事情发生(没有更新,没有例外)。