Rails非表下拉列表 [英] Rails non-table drop down list

查看:99
本文介绍了Rails非表下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个下拉列表,用户可以选择每周想要进入的星期几。价值观永远不会改变。这只是星期天,星期一,星期六吧?看起来像做一张桌子的工作比一般工作要多一些,我当然不需要创建,更新或删除它们的能力。有没有一个简单的方法来处理这样的事情?或者也许不是一周中的几天,它可能是关闭,停放,反向,中立,驱动器的状态。主要的是价值观永远不会改变。只有其中的几个。那为什么要做桌子呢?我想,有一种方法来创建一个已经有数据的模型,但是我可能是错误的。

I need to have a drop down list in which the user selects the day of the week they want to come in every week. The values will never change right. It's just Sunday, Monday, ..., Saturday right? It seems like more work than needed to make a table and put the days of the week in. I certainly don't need the ability to create, update or delete them. Is there a simple way to handle something like this? Or maybe instead of days of the week it could be status like off, park, reverse, neutral, drive. The main thing is that the values are never going to change. There are just a few of them. So why make a table? I am thinking that there is a way to create a model that already has data in it but I could be wrong.

推荐答案

为什么创建模型?只需使用选择。

Why create a model? Just use select.

DAYS = ['Monday', 'Tuesday', 'Wednesday', ...]
select(:event, :day, DAYS)

通常更好的做法是将常量放在

It's usually better practice to place the constant in the relevant model and use it from there.

在您的模型中:

class Event < ActiveRecord::Base
  DAYS = ['Monday', 'Tuesday', 'Wednesday', ...]
end

然后在您的视图中:

select(:event, :day, Event::DAYS)

这是另一个技巧我使用了很多:

and here's another trick I use a lot:

select(:event, :day, Event::DAYS.collect {|d| [d, Event::DAYS.index(d)]})

这篇关于Rails非表下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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