Rails 3:f.select-options_for_select [英] Rails 3: f.select - options_for_select

查看:101
本文介绍了Rails 3:f.select-options_for_select的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Ruby on Rails3应用程序上有一个带有下拉菜单的表单,这是我当前用于选择选项的代码:

I have a form on my Ruby on Rails3 Application with a drop menu, this is my current code for the select option:

<%= f.select :phone_type, options_for_select(["Select One", "Cell", "Work", "Office", "Home", "Other"],:disabled => ["Select One"]), :class => 'genForm_dropBox' %>

据我了解,当某人打开页面时,该选项应具有选择一个"作为默认选项,但是如果他们没有选择其他选项之一,则在他们单击提交"时会显示错误.

From my understanding this should have "Select One" as the default option when someone opens the page, but if they don't select one of the other options an error displays when they hit submit.

在Safari,Chrome和IE7等浏览器中都是如此,但在Firefox和IE8中,由于选择一个"已禁用,因此它显示"Cell"作为第一个选项.

This is true in Browsers like Safari and Chrome and IE7, but in Firefox and IE8 it shows "Cell" as the first option as Select One is disabled.

我希望它默认显示选择一个",但是当他们提交表单时,将其作为不可用的选项.我是否需要将此脚本编写到控制器或模型中?还是我用错误的形式将此代码编码?

I'd like it to display "Select One" by default, but have it as an unusable option when they submit the form. Do I need to script this into the controller, or model? or do I have this coded in the form wrong?

推荐答案

对于那些希望整合此功能的人,我从模型的角度出发采用了一种新方法.由于必须填写所有字段才能使用户提交而不接收错误警报,因此我为提交一个"选项提供了默认值"nothing".您可以看一下下面的代码,看看我是怎么做到的.

for those looking to incorporate this feature, I've taken a new approach from the model end of things. Being that all fields are required to be filled out in order for the user to submit and not receive an error alert, I gave the "Submit One" option a default value of nothing. You can take a look at the following code to see how I did that.

<%= f.select :phone_type, options_for_select([["Select One", ""], "Cell", "Work", "Office", "Home", "Other"]), :class => 'genForm_dropBox' %>

这篇关于Rails 3:f.select-options_for_select的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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