質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Ruby on Rails 5

Ruby on Rails 5は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Q&A

解決済

1回答

436閲覧

Rails5.2.3でpluralize()が正しく動きません

natecosan

総合スコア23

Ruby on Rails 5

Ruby on Rails 5は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

0グッド

1クリップ

投稿2019/05/26 01:32

以下の環境でアプリを作っているのですが、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>&lt;a href=\"http://example.com/\"&gt;Example&lt;/a&gt;</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

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

localeオプションに:enを指定すると、おそらく正常に動作すると思います。
localeオプションが指定されていないとI18n.localeが設定されるので、:jaが指定されているものと思われます。

[1] pry(main)> helper.pluralize(1, "error", locale: :en) => "1 error" [2] pry(main)> helper.pluralize(2, "error", locale: :en) => "2 errors"

投稿2019/05/26 03:38

mottox2

総合スコア299

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

natecosan

2019/05/26 12:43

ありがとうございます!!解決しました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問