![](/img/trans.png)
[英]Why does declarative_authorization break the functionality of my controller? - Rails 3
[英]Why does updating Rails from '6.0.4.8' to '6.1.6.1' break "^" string functionality?
我正在將我的項目從 Rails '6.0.4.8' 升級到'6.1.6.1'。 這樣做會破壞我的許多 rspec 測試(正如我所預料的那樣)。 但我似乎無法弄清楚為什么我有很多表格:
Failure/Error:
expect(log.errors.full_messages).to(include('error message'))
expected ["attribute name ^error message"] to include "error message"
顯然,根據測試類型略有不同,但我不打算在這里發布全部 50 個。 據我了解,“^”應該修剪字符串以僅包含以下所有內容(即,去掉屬性名稱並保留錯誤消息。這在較新的版本中是否已更改?無論如何,這是檢查為了包含,不是完全匹配,所以奇怪的是它失敗了。
這是我更新的軟件包:
-gem 'rails', '6.0.4.8'
+gem 'rails', '6.1.6.1'
-gem 'acts-as-taggable-on', '~> 6.0'
+gem 'acts-as-taggable-on', '~> 7.0'
- gem 'rspec-rails', '4.0.0.beta3'
+ gem 'rspec-rails', '4.1.2'
- actioncable (6.0.4.8)
- actionpack (= 6.0.4.8)
+ actioncable (6.1.6.1)
+ actionpack (= 6.1.6.1)
+ activesupport (= 6.1.6.1)
- actionmailbox (6.0.4.8)
- actionpack (= 6.0.4.8)
- activejob (= 6.0.4.8)
- activerecord (= 6.0.4.8)
- activestorage (= 6.0.4.8)
- activesupport (= 6.0.4.8)
+ actionmailbox (6.1.6.1)
+ actionpack (= 6.1.6.1)
+ activejob (= 6.1.6.1)
+ activerecord (= 6.1.6.1)
+ activestorage (= 6.1.6.1)
+ activesupport (= 6.1.6.1)
- actionmailer (6.0.4.8)
- actionpack (= 6.0.4.8)
- actionview (= 6.0.4.8)
- activejob (= 6.0.4.8)
+ actionmailer (6.1.6.1)
+ actionpack (= 6.1.6.1)
+ actionview (= 6.1.6.1)
+ activejob (= 6.1.6.1)
+ activesupport (= 6.1.6.1)
- actionpack (6.0.4.8)
- actionview (= 6.0.4.8)
- activesupport (= 6.0.4.8)
- rack (~> 2.0, >= 2.0.8)
+ actionpack (6.1.6.1)
+ actionview (= 6.1.6.1)
+ activesupport (= 6.1.6.1)
+ rack (~> 2.0, >= 2.0.9)
- actiontext (6.0.4.8)
- actionpack (= 6.0.4.8)
- activerecord (= 6.0.4.8)
- activestorage (= 6.0.4.8)
- activesupport (= 6.0.4.8)
+ actiontext (6.1.6.1)
+ actionpack (= 6.1.6.1)
+ activerecord (= 6.1.6.1)
+ activestorage (= 6.1.6.1)
+ activesupport (= 6.1.6.1)
- actionview (6.0.4.8)
- activesupport (= 6.0.4.8)
+ actionview (6.1.6.1)
+ activesupport (= 6.1.6.1)
- activejob (6.0.4.8)
- activesupport (= 6.0.4.8)
+ activejob (6.1.6.1)
+ activesupport (= 6.1.6.1)
- activemodel (6.0.4.8)
- activesupport (= 6.0.4.8)
- activerecord (6.0.4.8)
- activemodel (= 6.0.4.8)
- activesupport (= 6.0.4.8)
- activestorage (6.0.4.8)
- actionpack (= 6.0.4.8)
- activejob (= 6.0.4.8)
- activerecord (= 6.0.4.8)
- marcel (~> 1.0.0)
- activesupport (6.0.4.8)
+ activemodel (6.1.6.1)
+ activesupport (= 6.1.6.1)
+ activerecord (6.1.6.1)
+ activemodel (= 6.1.6.1)
+ activesupport (= 6.1.6.1)
+ activestorage (6.1.6.1)
+ actionpack (= 6.1.6.1)
+ activejob (= 6.1.6.1)
+ activerecord (= 6.1.6.1)
+ activesupport (= 6.1.6.1)
+ marcel (~> 1.0)
+ mini_mime (>= 1.1.0)
+ activesupport (6.1.6.1)
- i18n (>= 0.7, < 2)
- minitest (~> 5.1)
- tzinfo (~> 1.1)
- zeitwerk (~> 2.2, >= 2.2.2)
- acts-as-taggable-on (6.5.0)
- activerecord (>= 5.0, < 6.1)
+ i18n (>= 1.6, < 2)
+ minitest (>= 5.1)
+ tzinfo (~> 2.0)
+ zeitwerk (~> 2.3)
+ acts-as-taggable-on (7.0.0)
+ activerecord (>= 5.0, < 6.2)
- autoprefixer-rails (10.4.2.0)
+ autoprefixer-rails (10.4.7.0)
- bcrypt (3.1.17)
+ bcrypt (3.1.18)
- bootsnap (1.11.1)
+ bootsnap (1.13.0)
- capybara (3.36.0)
+ capybara (3.37.1)
- erubi (1.10.0)
- excon (0.92.3)
+ erubi (1.11.0)
+ excon (0.92.4)
- faker (2.20.0)
+ faker (2.22.0)
- faraday (1.10.0)
- faraday-em_http (~> 1.0)
- faraday-em_synchrony (~> 1.0)
- faraday-excon (~> 1.1)
- faraday-httpclient (~> 1.0)
- faraday-multipart (~> 1.0)
- faraday-net_http (~> 1.0)
- faraday-net_http_persistent (~> 1.0)
- faraday-patron (~> 1.0)
- faraday-rack (~> 1.0)
- faraday-retry (~> 1.0)
+ faraday (2.4.0)
+ faraday-net_http (~> 2.0)
- faraday-em_http (1.0.0)
- faraday-em_synchrony (1.0.0)
- faraday-excon (1.1.0)
- faraday-httpclient (1.0.1)
- faraday-multipart (1.0.3)
- multipart-post (>= 1.2, < 3)
- faraday-net_http (1.0.1)
- faraday-net_http_persistent (1.2.0)
- faraday-patron (1.0.0)
- faraday-rack (1.0.0)
- faraday-retry (1.0.3)
+ faraday-net_http (2.1.0)
+ faraday-retry (2.0.0)
+ faraday (~> 2.0)
- fog-google (1.18.0)
+ fog-google (1.19.0)
- gapic-common (0.8.0)
- faraday (~> 1.3)
+ gapic-common (0.11.1)
+ faraday (>= 1.9, < 3.a)
+ faraday-retry (>= 1.0, < 3.a)
- googleapis-common-protos (>= 1.3.11, < 2.a)
- googleapis-common-protos-types (>= 1.0.6, < 2.a)
- googleauth (>= 0.17.0, < 2.a)
+ googleapis-common-protos (>= 1.3.12, < 2.a)
+ googleapis-common-protos-types (>= 1.3.1, < 2.a)
+ googleauth (~> 1.0)
- globalize (6.1.0)
+ globalize (6.2.1)
- google-apis-compute_v1 (0.34.0)
- google-apis-core (>= 0.4, < 2.a)
- google-apis-core (0.4.2)
+ google-apis-compute_v1 (0.45.0)
+ google-apis-core (>= 0.7, < 2.a)
+ google-apis-core (0.7.0)
- google-apis-dns_v1 (0.19.0)
- google-apis-core (>= 0.4, < 2.a)
- google-apis-iamcredentials_v1 (0.10.0)
- google-apis-core (>= 0.4, < 2.a)
- google-apis-monitoring_v3 (0.24.0)
- google-apis-core (>= 0.4, < 2.a)
- google-apis-pubsub_v1 (0.17.0)
- google-apis-core (>= 0.4, < 2.a)
- google-apis-sqladmin_v1beta4 (0.27.0)
- google-apis-core (>= 0.4, < 2.a)
- google-apis-storage_v1 (0.13.0)
- google-apis-core (>= 0.4, < 2.a)
+ google-apis-dns_v1 (0.23.0)
+ google-apis-core (>= 0.7, < 2.a)
+ google-apis-iamcredentials_v1 (0.13.0)
+ google-apis-core (>= 0.7, < 2.a)
+ google-apis-monitoring_v3 (0.32.0)
+ google-apis-core (>= 0.7, < 2.a)
+ google-apis-pubsub_v1 (0.24.0)
+ google-apis-core (>= 0.7, < 2.a)
+ google-apis-sqladmin_v1beta4 (0.32.0)
+ google-apis-core (>= 0.7, < 2.a)
+ google-apis-storage_v1 (0.17.0)
+ google-apis-core (>= 0.7, < 2.a)
- google-cloud-error_reporting-v1beta1 (0.4.5)
- gapic-common (>= 0.7, < 2.a)
+ google-cloud-error_reporting-v1beta1 (0.5.0)
+ gapic-common (>= 0.10, < 2.a)
- google-cloud-functions (1.1.3)
+ google-cloud-functions (1.2.0)
- google-cloud-functions-v1 (0.6.0)
- gapic-common (>= 0.7, < 2.a)
+ google-cloud-functions-v1 (0.8.0)
+ gapic-common (>= 0.10, < 2.a)
- google-cloud-logging (2.2.2)
+ google-cloud-logging (2.3.1)
- google-cloud-logging-v2 (0.7.0)
- gapic-common (>= 0.7, < 2.a)
+ google-cloud-logging-v2 (0.8.1)
+ gapic-common (>= 0.10, < 2.a)
- google-cloud-storage (1.36.2)
+ google-cloud-storage (1.38.0)
- google-apis-storage_v1 (~> 0.1)
+ google-apis-storage_v1 (~> 0.17.0)
- google-cloud-trace (0.41.3)
+ google-cloud-trace (0.42.1)
- google-cloud-trace-v1 (0.3.5)
- gapic-common (>= 0.7, < 2.a)
+ google-cloud-trace-v1 (0.4.0)
+ gapic-common (>= 0.10, < 2.a)
- google-cloud-trace-v2 (0.3.5)
- gapic-common (>= 0.7, < 2.a)
+ google-cloud-trace-v2 (0.4.0)
+ gapic-common (>= 0.10, < 2.a)
- google-protobuf (3.20.1)
+ google-protobuf (3.21.4)
- googleapis-common-protos-types (1.3.1)
+ googleapis-common-protos-types (1.3.2)
- googleauth (1.1.3)
+ googleauth (1.2.0)
- grpc (1.45.0)
+ grpc (1.48.0)
- grpc-google-iam-v1 (1.1.0)
+ grpc-google-iam-v1 (1.1.1)
- http-cookie (1.0.4)
+ http-cookie (1.0.5)
- i18n (1.10.0)
+ i18n (1.12.0)
- jquery-rails (4.4.0)
+ jquery-rails (4.5.0)
- json (2.6.1)
- jwt (2.3.0)
+ json (2.6.2)
+ jwt (2.4.1)
- minitest (5.15.0)
+ minitest (5.16.2)
- momentjs-rails (2.29.1.1)
+ momentjs-rails (2.29.4.1)
- msgpack (1.5.1)
+ msgpack (1.5.4)
- multipart-post (2.1.1)
- mysql2 (0.5.3)
+ mysql2 (0.5.4)
- nokogiri (1.13.6)
+ nokogiri (1.13.8)
- oj (3.13.11)
+ oj (3.13.19)
- pdf-reader (2.9.2)
+ pdf-reader (2.10.0)
- phony (2.19.11)
+ phony (2.20.0)
- rack (2.2.3.1)
+ rack (2.2.4)
- rack-test (1.1.0)
- rack (>= 1.0, < 3)
- rails (6.0.4.8)
- actioncable (= 6.0.4.8)
- actionmailbox (= 6.0.4.8)
- actionmailer (= 6.0.4.8)
- actionpack (= 6.0.4.8)
- actiontext (= 6.0.4.8)
- actionview (= 6.0.4.8)
- activejob (= 6.0.4.8)
- activemodel (= 6.0.4.8)
- activerecord (= 6.0.4.8)
- activestorage (= 6.0.4.8)
- activesupport (= 6.0.4.8)
- bundler (>= 1.3.0)
- railties (= 6.0.4.8)
+ rack-test (2.0.2)
+ rack (>= 1.3)
+ rails (6.1.6.1)
+ actioncable (= 6.1.6.1)
+ actionmailbox (= 6.1.6.1)
+ actionmailer (= 6.1.6.1)
+ actionpack (= 6.1.6.1)
+ actiontext (= 6.1.6.1)
+ actionview (= 6.1.6.1)
+ activejob (= 6.1.6.1)
+ activemodel (= 6.1.6.1)
+ activerecord (= 6.1.6.1)
+ activestorage (= 6.1.6.1)
+ activesupport (= 6.1.6.1)
+ bundler (>= 1.15.0)
+ railties (= 6.1.6.1)
- rails-i18n (7.0.3)
+ rails-i18n (7.0.5)
- railties (6.0.4.8)
- actionpack (= 6.0.4.8)
- activesupport (= 6.0.4.8)
+ railties (6.1.6.1)
+ actionpack (= 6.1.6.1)
+ activesupport (= 6.1.6.1)
- rake (>= 0.8.7)
- thor (>= 0.20.3, < 2.0)
+ rake (>= 12.2)
+ thor (~> 1.0)
- regexp_parser (2.3.1)
- representable (3.1.1)
+ regexp_parser (2.5.0)
+ representable (3.2.0)
- rmagick (4.2.5)
+ rmagick (4.2.6)
- rspec-rails (4.0.0.beta3)
+ rspec-rails (4.1.2)
- rspec-core (~> 3.8)
- rspec-expectations (~> 3.8)
- rspec-mocks (~> 3.8)
- rspec-support (~> 3.8)
+ rspec-core (~> 3.10)
+ rspec-expectations (~> 3.10)
+ rspec-mocks (~> 3.10)
+ rspec-support (~> 3.10)
- rubocop (1.28.2)
+ rubocop (1.33.0)
+ json (~> 2.3)
- rexml
- rubocop-ast (>= 1.17.0, < 2.0)
+ rexml (>= 3.2.5, < 4.0)
+ rubocop-ast (>= 1.19.1, < 2.0)
- rubocop-ast (1.17.0)
+ rubocop-ast (1.19.1)
- rubocop-performance (1.13.3)
+ rubocop-performance (1.14.3)
- rubocop-rails (2.14.2)
+ rubocop-rails (2.15.2)
- rubocop-rspec (2.10.0)
- rubocop (~> 1.19)
+ rubocop-rspec (2.12.1)
+ rubocop (~> 1.31)
- sdoc (2.3.2)
+ sdoc (2.4.0)
- selenium-webdriver (4.1.0)
+ selenium-webdriver (4.3.0)
- rubyzip (>= 1.2.2)
- shopify_api (10.0.3)
+ rubyzip (>= 1.2.2, < 3.0)
+ websocket (~> 1.0)
+ shopify_api (11.0.1)
- signet (0.16.1)
+ signet (0.17.0)
- faraday (>= 0.17.5, < 3.0)
+ faraday (>= 0.17.5, < 3.a)
- sorbet-runtime (0.5.9959)
+ sorbet-runtime (0.5.10274)
- ssrf_filter (1.0.7)
+ ssrf_filter (1.0.8)
- thread_safe (0.3.6)
- tilt (2.0.10)
+ tilt (2.0.11)
- tzinfo (1.2.10)
- thread_safe (~> 0.1)
+ tzinfo (2.0.5)
+ concurrent-ruby (~> 1.0)
- unf_ext (0.0.8.1)
- unicode-display_width (2.1.0)
+ unf_ext (0.0.8.2)
+ unicode-display_width (2.2.0)
+ websocket (1.2.9)
- zeitwerk (2.5.4)
+ zeitwerk (2.6.0)
- acts-as-taggable-on (~> 6.0)
+ acts-as-taggable-on (~> 7.0)
- rails (= 6.0.4.8)
+ rails (= 6.1.6.1)
- rspec-rails (= 4.0.0.beta3)
+ rspec-rails (= 4.1.2)
讓我知道我是否缺少任何菜鳥細節。
像這樣可能非常重要的細節:
$ bundle exec ruby --version
ruby 2.7.5p203 (2021-11-24 revision f69aeb8314) [x86_64-linux-musl]
和這個:
$ cat /etc/os-release
NAME="Alpine Linux"
ID=alpine
VERSION_ID=3.15.4
PRETTY_NAME="Alpine Linux v3.15"
HOME_URL="https://alpinelinux.org/"
BUG_REPORT_URL="https://bugs.alpinelinux.org/"
為了更好的衡量:
$ bundle exec rspec --version
RSpec 3.11
- rspec-core 3.11.0
- rspec-expectations 3.11.0
- rspec-mocks 3.11.1
- rspec-rails 4.1.2
- rspec-support 3.11.0
現在隨意把我撕成碎片;-)
您觀察到的錯誤消息以^
開頭的行為在調用full_messages
時省略了屬性名稱,這從來不是 Rails 上的默認 Ruby 功能。
相反,像custom-err-msg
這樣的插件或基於該插件的 gems 被用來將此行為添加到 Rails。 其他選項是覆蓋 Rails 方法以添加此功能。 有關過去如何完成此操作的更多示例,請參閱這個老問題。
由於 Rails 6.1 上的 Ruby 大多數這些技術不再起作用,因為在 Rails 6.1 上的 Ruby 錯誤不再是簡單的 hash 屬性鍵和消息字符串。 這個拉取請求引入了一個ActiveModel::Error
model,它使用 Rails 6.1 封裝錯誤消息。
實現您正在尋找的行為的推薦方法是利用本地化框架並覆蓋 Rails 在調用full_messages
時使用的默認錯誤消息格式。 這可以在不同級別、應用范圍內完成,僅針對一個 model,甚至僅針對一個特定屬性。
例如,當您想省略屬性名稱時,只想顯示Post#title
錯誤的錯誤消息,然后將以下內容添加到您的config/locales.en.yml
:
# config/locales/en.yml
en:
activerecord:
errors:
models:
post:
attributes:
title:
format: "%{message}" # default is "#{attribute} #{message}"
我建議查看這篇博客文章,其中提供了大量示例,如何根據您的需要自定義錯誤消息。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.