如何在 sinatra 视图中列出可能的枚举符号?
假设我有一个这样的模型:
class Animal
include DataMapper::Resource
property :id, Serial
property :type, Enum[ :cat, :bat, :rabbit, :zebra]
end
假设有一条指向 erb 模板的路线,用于添加更多动物和动物。 @animal = session[:animal]
我将如何创建动物类型列表?
...
<form>
<% @animal.type.each do |animal| %>
<select>
<option value="<%= @animal.type" %></option>
</select>
<% end %>
</form>
(显然,这段代码并没有达到我想要的效果,但我希望它能让它更加清晰。)
Say I have a model like so:
class Animal
include DataMapper::Resource
property :id, Serial
property :type, Enum[ :cat, :bat, :rabbit, :zebra]
end
Assuming there is a route pointing to an erb template for adding more animals & @animal = session[:animal]
how would I create a list of animal types?
...
<form>
<% @animal.type.each do |animal| %>
<select>
<option value="<%= @animal.type" %></option>
</select>
<% end %>
</form>
(Obviously that bit of code doesn't do what I am looking for, but I hope it makes it a little more clear.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
属性上有 flags 选项,可用于查找枚举值。我不知道这个记录在哪里 - 我发现它 此处。所以你可以这样做:
我想你可以将其概括为一个辅助方法。
There is flags option on the property that you can use to lookup the enum values. I don't know where this is documented - I found it here. So you could do something like this:
I guess you could generalise this into a helper method.