使用update!导致的更新时候的错误信息不显示 ruby on rails

在图片管理里添加了校验方法之后,发现在更新的时候页面不显示校验报错的信息

class Picture < ApplicationRecord
belongs_to :imageable, polymorphic: true validates_uniqueness_of :name, scope: [:imageable_id, :imageable_type]
end

update!在校验的时候会抛出异常,导致ajax操作中断,

Started PATCH "/components/11" for 127.0.0.1 at 2016-09-06 14:31:08 +0800
Processing by ComponentsController#update as JS
Parameters: {"utf8"=>"✓", "component"=>{"name"=>"", "alias"=>"", "pictures_attributes"=>[{"id"=>"", "name"=>"poster", "url"=>"0510000057CE600F6714C06411004555", "md5"=>"9ff2d30b17de8fc75163faff99c18afc"}, {"id"=>"", "name"=>"poster", "url"=>"0510000057CE60BF6714C063340BE1DC", "md5"=>"c5641765c087043ddc937ac5938c98d2"}, {"name"=>"poster", "url"=>"", "md5"=>""}], "release_id"=>"", "component_type_id"=>"", "x_axis"=>"", "y_axis"=>"", "width"=>"", "height"=>"", "unitary"=>"true", "ratio"=>"", "column"=>"", "hints"=>{"left"=>"", "mid"=>"", "right"=>""}, "remark"=>""}, "commit"=>"更新模块", "id"=>""}
User Load (0.3ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 ORDER BY `users`.`id` ASC LIMIT 1
Component Load (0.2ms) SELECT `components`.* FROM `components` WHERE `components`.`id` = 11 LIMIT 1
(0.1ms) BEGIN
Picture Load (0.2ms) SELECT `pictures`.* FROM `pictures` WHERE `pictures`.`imageable_id` = 11 AND `pictures`.`imageable_type` = 'Component' AND `pictures`.`id` IN (33, 34)
Picture Exists (0.4ms) SELECT 1 AS one FROM `pictures` WHERE `pictures`.`name` = BINARY 'poster' AND (`pictures`.`id` != 34) AND `pictures`.`imageable_type` = 'Component' LIMIT 1
Component Exists (0.2ms) SELECT 1 AS one FROM `components` WHERE `components`.`name` = BINARY '' AND (`components`.`id` != 11) AND `components`.`release_id` = 1 LIMIT 1
(0.2ms) ROLLBACK
Completed 422 Unprocessable Entity in 23ms (ActiveRecord: 3.4ms) ActiveRecord::RecordInvalid (验证失败: Pictures name已经被使用):

原因是在controller的update方法里加了!,变成了update!(component_params), 去掉后的方法如下

  def update
if @component.update(component_params)
redirect_to @component, notice: 'Component was successfully updated.'
else
render :error
end
end

去掉后的log如下

Started PATCH "/components/11" for 127.0.0.1 at 2016-09-06 15:21:37 +0800
Processing by ComponentsController#update as JS
Parameters: {"utf8"=>"✓", "component"=>{"name"=>"", "alias"=>"", "pictures_attributes"=>[{"id"=>"", "name"=>"poster", "url"=>"0510000057CE600F6714C06411004555", "md5"=>"9ff2d30b17de8fc75163faff99c18afc"}, {"id"=>"", "name"=>"poster", "url"=>"0510000057CE60BF6714C063340BE1DC", "md5"=>"c5641765c087043ddc937ac5938c98d2"}, {"name"=>"poster", "url"=>"", "md5"=>""}], "release_id"=>"", "component_type_id"=>"", "x_axis"=>"", "y_axis"=>"", "width"=>"", "height"=>"", "unitary"=>"true", "ratio"=>"", "column"=>"", "hints"=>{"left"=>"", "mid"=>"", "right"=>""}, "remark"=>""}, "commit"=>"更新模块", "id"=>""}
User Load (0.2ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 ORDER BY `users`.`id` ASC LIMIT 1
Component Load (0.1ms) SELECT `components`.* FROM `components` WHERE `components`.`id` = 11 LIMIT 1
(0.1ms) BEGIN
Picture Load (0.1ms) SELECT `pictures`.* FROM `pictures` WHERE `pictures`.`imageable_id` = 11 AND `pictures`.`imageable_type` = 'Component' AND `pictures`.`id` IN (33, 34)
Picture Exists (0.2ms) SELECT 1 AS one FROM `pictures` WHERE `pictures`.`name` = BINARY 'poster' AND (`pictures`.`id` != 34) AND `pictures`.`imageable_type` = 'Component' LIMIT 1
Component Exists (0.2ms) SELECT 1 AS one FROM `components` WHERE `components`.`name` = BINARY '' AND (`components`.`id` != 11) AND `components`.`release_id` = 1 LIMIT 1
(0.1ms) ROLLBACK
Rendering components/error.js.erb
Picture Load (0.2ms) SELECT `pictures`.* FROM `pictures` WHERE `pictures`.`imageable_id` = 11 AND `pictures`.`imageable_type` = 'Component'
Rendered shared/_image.html.erb (2.6ms)
Rendered shared/_image.html.erb (1.4ms)
Rendered shared/_image.html.erb (1.5ms)
(0.2ms) SELECT `releases`.`version_name`, `releases`.`id` FROM `releases` ORDER BY `releases`.`version_name` DESC
(0.2ms) SELECT `component_types`.`name`, `component_types`.`id` FROM `component_types`
Rendered components/_form.html.erb (36.7ms)
Rendered components/error.js.erb (52.7ms)
Completed 200 OK in 111ms (Views: 87.3ms | ActiveRecord: 1.6ms)
上一篇:Shell文本处理四剑客


下一篇:zhenya moves from parents