以下の環境でアプリを作っているのですが、pluralize関数がうまく作動しません。
ヘルパーが読み込めていない?ということなのでしょうか。。。
よろしくお願いいたします。
実行環境
rails 5.2.3
ruby 2.6.1p33
該当のソースコード
ruby
1$ rails c 2Running via Spring preloader in process 16483 3Loading development environment (Rails 5.2.3) 4irb(main):001:0> helper.pluralize(1, "error") 5=> "1 error" 6irb(main):002:0> helper.pluralize(2, "error") 7=> "2 error" 8irb(main):003:0> helper.pluralize(5, "error") 9=> "5 error" 10irb(main):004:0>
###試したこと
text_helper.rb を見てみましたが記載されているようです。
ruby
1# frozen_string_literal: true 2 3require "active_support/core_ext/string/filters" 4require "active_support/core_ext/array/extract_options" 5 6module ActionView 7 # = Action View Text Helpers 8 module Helpers #:nodoc: 9 # The TextHelper module provides a set of methods for filtering, formatting 10 # and transforming strings, which can reduce the amount of inline Ruby code in 11 # your views. These helper methods extend Action View making them callable 12 # within your template files. 13 # 14 # ==== Sanitization 15 # 16 # Most text helpers that generate HTML output sanitize the given input by default, 17 # but do not escape it. This means HTML tags will appear in the page but all malicious 18 # code will be removed. Let's look at some examples using the +simple_format+ method: 19 # 20 # simple_format('<a href="http://example.com/">Example</a>') 21 # # => "<p><a href=\"http://example.com/\">Example</a></p>" 22 # 23 # simple_format('<a href="javascript:alert(\'no!\')">Example</a>') 24 # # => "<p><a>Example</a></p>" 25 # 26 # If you want to escape all content, you should invoke the +h+ method before 27 # calling the text helper. 28 # 29 # simple_format h('<a href="http://example.com/">Example</a>') 30 # # => "<p><a href=\"http://example.com/\">Example</a></p>" 31 module TextHelper 32 extend ActiveSupport::Concern 33 34 include SanitizeHelper 35 include TagHelper 36 include OutputSafetyHelper 37 38 (中略) 39 40 # Attempts to pluralize the +singular+ word unless +count+ is 1. If 41 # +plural+ is supplied, it will use that when count is > 1, otherwise 42 # it will use the Inflector to determine the plural form for the given locale, 43 # which defaults to I18n.locale 44 # 45 # The word will be pluralized using rules defined for the locale 46 # (you must define your own inflection rules for languages other than English). 47 # See ActiveSupport::Inflector.pluralize 48 # 49 # pluralize(1, 'person') 50 # # => 1 person 51 # 52 # pluralize(2, 'person') 53 # # => 2 people 54 # 55 # pluralize(3, 'person', plural: 'users') 56 # # => 3 users 57 # 58 # pluralize(0, 'person') 59 # # => 0 people 60 # 61 # pluralize(2, 'Person', locale: :de) 62 # # => 2 Personen 63 def pluralize(count, singular, plural_arg = nil, plural: plural_arg, locale: I18n.locale) 64 word = if (count == 1 || count.to_s =~ /^1(.0+)?$/) 65 singular 66 else 67 plural || singular.pluralize(locale) 68 end 69 70 "#{count || 0} #{word}" 71 end 72 73 (中略) 74 end 75 end 76end 77
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/05/26 12:43