Tag: 枚举

Rails 5:i18n用于命名空间模型的枚举

在命名空间模型中,我对i18n有点困惑。 目前我在模型/app/media/medias.rb中有这样的行: enum territory: { local: 1, global: 2 } 在模态窗口/views/media/medias/_newmedia.html.erb我有这个: 在/config/locales/models/medias.en.yml我有这个: en: media: medias: territories: local: Local_EN global: Global_EN 在控制台中我看到查询完成,没有抛出错误,但我的视图没有出现。 当我更改为简单的Media::Medias.territories.keys我在视图中的下拉列表中显示了键值。 我该如何解决这个问题? 谢谢! 到目前为止,我已经尝试从这个答案中实现代码 。 在视图中它很好地提供了翻译,但是在创建操作时我收到错误,因为它试图保存不是枚举键,而是本地化值。 解决方案 好吧,看来这个答案在我的案例中是解决方案 。 如果有人需要,我的示例应该如何使用命名空间模型: 1)在模型/app/media/medias.rb中 enum territory: { local: 1, global: 2 } def self.territory_attributes_for_select territories.map do |territory, _| [I18n.t(“activerecord.attributes.#{model_name.i18n_key}.territories.#{territory}”), territory] end end 2)然后在视图中部分/views/media/medias/_newmedia.html.erb 3)最后在/config/locales/en.yml en: activerecord: attributes: […]

Rails 4 – 如何使用枚举?

我正在尝试在Rails 4上制作应用程序。 我发布了这个问题并得到了一些建议: Rails 4 -Simple Form如何保存键和显示值 我想弄清楚如何实现这个建议。 目前,我有一个偏好模型: enum self_governance: { tier_1: 1, tier_2: 2, tier_3: 3, tier_4: 4, tier_5: 5 } enum autonomy: { tier_11: 1, tier_21: 2, tier_31: 3, tier_41: 4, tier_51: 5 } 在我的偏好forms中,我有: 我有一个偏好显示视图: 当我保存所有这些并尝试它时,我收到此错误: NoMethodError at /preferences/1/edit undefined method `self_governance’ for # Did you mean? self_governances 任何人都可以看到如何使这项工作? 我是否可能需要在首选模型中为枚举添加def / […]

Rails枚举validation不起作用但引发ArgumentError

这里创建了一个线程,但它没有解决我的问题。 我的代码是: course.rb class Course < ApplicationRecord COURSE_TYPES = %i( trial limited unlimited ) enum course_type: COURSE_TYPES validates_inclusion_of :course_type, in: COURSE_TYPES end courses_controller.rb class CoursesController < ApiController def create course = Course.new(course_params) # <– Exception here if course.save # <– But I expect the process can go here render json: course, status: :ok else render […]

从表单中选择枚举以设置角色

Ruby on Rails 4.1 我正在使用具有枚举角色的Devise。 它在创建用户时当前设置了defualt角色。 我想在创建用户设置枚举角色的表单中添加一个字段。 我读过这篇文章,但没有说明如何利用新角色。 这是User类 devise :database_authenticatable, :registerable, :confirmable, :recoverable, :rememberable, :trackable, :validatable enum role: [:user, :vip, :admin, :developer, :marketing, :support, :translator] after_initialize :set_default_role, :if => :new_record? def set_default_role self.role ||= :user end 这是我尝试选择一个枚举角色的表单的一部分: 错误: NoMethodError – undefined method `enum’ for [“user”, 0]:Array: actionview (4.1.1) lib/action_view/helpers/form_options_helper.rb:761:in `value_for_collection’ 我之前从未使用过enum, 文档也没有certificate有用。 如何显示枚举选项?

Rails – 枚举字段的测试validation

我正在使用Rails 4枚举,我想正确测试它们,所以我为枚举字段设置了这些测试: it { should validate_inclusion_of(:category).in_array(%w[sale sale_with_tax fees lease tax_free other payroll]) } it { should validate_inclusion_of(:type).in_array(%w[receivable payable]) } 这是他们正在validation的模型: class Invoice < ActiveRecord::Base belongs_to :user enum category: [:sale, :sale_with_tax, :fees, :lease, :tax_free, :other, :payroll] enum type: [:receivable, :payable] validates :user, presence: true validates :issue_date, presence: true validates :series, presence: true validates :folio, presence: true […]

将枚举值映射到字符串类型而不是整数的可能性

枚举属性很棒,我想使用它们。 但是将枚举值映射到整数将使得难以维护代码和数据库。 此外,我的数据库将与我的代码高度耦合,我认为我应该认为这是一件坏事。 我知道我可以使用哈希来组织具有键/值对的枚举属性,但是能够使用数组并映射到数据库中的字符串值仍然会好得多。 有默认情况下将枚举映射到字符串的方法吗?

在ruby中指定foreach枚举器的起始点

我有一个rake文件,它从外部CSV文件中提取数据并通过它枚举: CSV.foreach(file, :headers => true) do |row| 在电子表格中指定起点的最有效方法(在ruby中)是什么? :headers => true允许我从第二行开始导入,但如果我想开始第20行怎么办?

在rails上的枚举

我是一名C#程序员,我正在寻找ruby on rails。 但是我可能会遇到一些麻烦,或者说有一些想法。 我有一个对象投票,该对象可以是Pro,Neutral或con。 我会正常地使投票对象有一个像这样的字段 private VoteEnum voteEnum = VoteEnum.Neutral 我怎么能在ruby中实现这一点。 我找到了一些例子: def MyClass < ActiveRecord::Base ACTIVE_STATUS = "active" INACTIVE_STATUS = "inactive" PENDING_STATUS = "pending" end 然后,当使用来自另一个类的模型时,我引用了常量 @model.status = MyClass::ACTIVE_STATUS @model.save 这似乎对我来说是正确的,但我的主要问题是我如何告诉模型状态是枚举或constain的类型.. 我希望你能理解我的问题,并希望你能帮助我解决这个问题。

ActiveRecord并使用reject方法

我有一个模型可以从特定城市获取所有游戏。 当我得到那些游戏时,我想过滤它们,我想使用reject方法,但我遇到了一个我想要理解的错误。 # STEP 1 – Model class Matches < ActiveRecord::Base def self.total_losses(cities) reject{ |a| cities.include?(a.winner) }.count end end # STEP 2 – Controller @games = Matches.find_matches_by("Toronto") # GOOD! – Returns ActiveRecord::Relation # STEP 3 – View cities = ["Toronto", "NYC"] @games.total_losses(cities) # FAIL – undefined method reject for # # STEP 3 – View […]

在Ruby中使用to_enum创建可枚举对象的优点是什么?

为什么要使用to_enum方法而不是直接使用对象,在Ruby中创建对象的代理引用? 我想不出任何实际用途,试图理解这个概念以及有人可能使用它的地方,但我看到的所有例子看起来都非常微不足道。 例如,为什么使用: “hello”.enum_for(:each_char).map {|c| c.succ } 代替 “hello”.each_char.map {|c| c.succ } 我知道这是一个非常简单的例子,有没有人有任何现实世界的例子?