gpt4 book ai didi

ruby-on-rails - Ruby:如何根据可变数量在 Active Admin 面板中将单词复数/单数化(Active Admin,rails 3.2,ruby)

转载 作者:数据小太阳 更新时间:2023-10-29 08:17:24 27 4
gpt4 key购买 nike

我是 Ruby on Rails 的新手,我无法根据单词的数量更改单词的结尾。

靠近下面代码的顶部,你会看到我写了

h3 "The deal has #{deal.prizes.where(:prize_type => u).count} #{u}

此处的“u”取名为 PRIZE_TYPE 的数组中的值之一。我该怎么做才能做到这一点

  • 如果计数= 1,则显示“该交易有 1 个累积奖金”

  • 如果count=4,则显示“该deal有4个jackpot prizeS

注意:计数不能为 = 0 或 < 0

这是我的代码:

panel "Details of Prizes" do

PRIZE_TYPES.each do |u|

table_for deal.prizes.where(:prize_type => u) do |t|
h3 "The deal has #{deal.prizes.where(:prize_type => u).count} #{u}s
initially set in the campaign for a total value of #{custom_number_to_currency (deal.category_prizes_total_initial_value(u)) }", class: 'title-within-table'
if deal.prizes.where(:prize_type => u).count > 0 # if there is at least one record of 'jackpot prize'
t.column("Prize") { |prize| link_to( image_tag( prize.prize_image_url, class: 'main_img_in_admin_interface' ), admin_prize_path(prize), target: :blank ) }
t.column("Name") { |prize| link_to prize.prize_name, admin_prize_path(prize), target: :blank }
t.column("Category") { |prize| prize.prize_category }
t.column("Initial quantity") { |prize| prize.prize_initial_stock_quantity }

end # end of Prize.each...

end # end of panel "details of prizes"

和 PRIZE_TYPE 常量:

PRIZE_TYPES = ["Jackpot prize","Consolation prize", "awesome prize"]

最佳答案

你可以使用 ruby​​ 的复数形式:

prize_type.pluralize(count)
"Jackpot prize".pluralize(1)
#> Jackpot prize
"Jackpot prize".pluralize(2)
#> Jackpot prizes

ActiveSupport::Inflector.pluralize如果您想自己检查:

prize_type.pluralize
"Jackpot prize".pluralize
#> "Jackpot Prizes"

或者 rails 有使用组合的复数形式 Inflector和一些基本逻辑:

pluralize(count, prize_type)
pluralize(1, prize_type)
#> 1 Jackpot prize
pluralize(2, prize_type)
#> 2 Jackpot prizes

关于ruby-on-rails - Ruby:如何根据可变数量在 Active Admin 面板中将单词复数/单数化(Active Admin,rails 3.2,ruby),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21440091/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com