Rails 4 - 如何使用枚举? [英] Rails 4 - how to use enum?

查看:145
本文介绍了Rails 4 - 如何使用枚举?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我发布了这个问题,并提出了一些建议: Rails 4 -Simple Form如何保存密钥和显示值



我试图找出如何实现这个建议。



目前,我有一个偏好模型: p>

 枚举self_governance:{
tier_1:1,
tier_2:2,
tier_3:3,
tier_4:4,
tier_5:5
}

枚举自治:{
tier_11:1,
tier_21:2,
tier_31:3,
tier_41:4,
tier_51:5
}

在我的偏好表单中,我有:

 <%= f.input:self_governance,as::select,label:选择您的治理方法,集合:Preference.self_governance.to_a.map {| p | [p.humanize,p]}%> 

我有一个偏好显示视图:

 <%= @ organisation.preference.self_governance.try(:humanize)%> 

当我保存所有这些并尝试它,我收到这个错误:

  / preferences / 1 / edit 
未定义方法的NoMethodError#< Class:0x007fde5b9fb500&
你的意思是? self_governances

任何人都可以看到如何使这项工作?



我可能需要添加def / end标签到首选项模型中的枚举?我没有任何使用代码枚举的经验

解决方案

你很接近:)修复是正确的在



您的选择正在调用

  Preference.self_governance .to_a.map {| p | [p.humanize,p]} 

而您的错误告诉您复数是错误的。请记住,如果您在单个对象上调用枚举,则将

  @ preferences.self_governance 

但是,如果您调用模型本身,首选项,并请求收集它是复数。

  Preference.self_governances 

因为枚举是特殊的,你的枚举可以只是数组,而不是散列:

 枚举self_governance :[tier_1,tier_2,tier_3,tier_4,tier_55] 

枚举自治:[tier_11,tier_21,tier_31,tier_41,tier_51]

您的观点将如下所示:

 <%= f.input :self_governance,as::select,label:选择你的治理方法,集合:Preference.self_governances.map {| key,value | [key.humanize,key]}%> 

它将存储数组的索引号,如magic:)


I'm trying to make an app on Rails 4.

I posted this question and got some advice: Rails 4 -Simple Form how to save key and display value

I am trying to figure out how to implement this advice.

At the moment, I have a preference model with:

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
                        }           

In my preference form, I have:

<%= f.input :self_governance, as: :select, label: "Select your governance approach", collection: Preference.self_governance.to_a.map { |p| [p.humanize, p] } %>

I have a preferences show view:

<%= @organisation.preference.self_governance.try(:humanize) %>

When I save all this and try it, I get this error:

NoMethodError at /preferences/1/edit
undefined method `self_governance' for #<Class:0x007fde5b9fb500>
Did you mean?  self_governances

Can anyone see how to make this work?

Do I maybe need to add def/end tags to the enum in the preference model? I don't have any experience with using the code 'enum'

解决方案

You're so close :) The fix is right in the error.

Your select is calling

Preference.self_governance.to_a.map { |p| [p.humanize, p] }

And your error tells you the pluralization is wrong. Remember that if you call enum on a single object, it will be

@preference.self_governance

But if you call on the model itself, Preference, and request a collection it's plural.

Preference.self_governances

Because enum is special, uour enum's could just be arrays, instead of hashes:

enum self_governance: [ tier_1, tier_2, tier_3, tier_4, tier_55 ]

enum autonomy: [ tier_11, tier_21, tier_31, tier_41, tier_51 ]

Your view would look like:

<%= f.input :self_governance, as: :select, label: "Select your governance approach", collection: Preference.self_governances.map { |key, value| [key.humanize, key] } %>

It will store the index number of the array, like magic :)

这篇关于Rails 4 - 如何使用枚举?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆