`

三种Select helper

阅读更多

select:<wbr></wbr>
  
select(object, method, choices, options = {}, html_options = {})
  
在ActionView::Helpers::FormOptionsHelper中定义



object是一個實體化變數,這裡很明顯的就是要擺上model物件嘛!
method則是object的一個屬性,也是資料表中的對應欄位
choices就是要被選的選項,可以是陣列或者是雜湊(Hash)
options與html_options則是一些選項在這邊來舉個例子吧

<%= select("project", "teacher_id", @teachers.collect{|t| 
  [t.name, t.id]}, { :include_blank => false }) %>
<%= 
  select("project", "student_id", {"CFC" => '1', "EF" => '2'}) 
  %>
第一個例子中,@teachers在Controller是這樣的
@teachers = Teacher.find(:all, 
  :select => 'id, name')

select_tag:<wbr></wbr>
  select_tag(name, option_tags = 
nil, options = {})
  
在ActionView::Helpers::FormTagHelper中定義

如果你很喜歡手動打option的話.. 
那用select_tag準沒錯啦!
在select_tag中,name將會是params所接收值所用的鍵
直接看範例

<%= select_tag 'user', "<option>CFC</option>" 
  %>
這時在Controller中將會用params[:user]來接收傳過來的值
但是select_tag也可以搭配options_for_select或者options_from_collection_for_select一起使用.. 
  來看一個範例吧
<%= select_tag('sid[]', 
  options_from_collection_for_select(@students, 'id', 'name'), :multiple => 
  true)%>
因為加上了:multiple,所以可以接受多值選擇,這時在Controller接收到的sid將會是一個陣列,這也是我所卡住的地方.. 
  (( 真丟臉

collection_select:<wbr></wbr>
  
collection_select(object, method, collection, value_method, text_method, options 
= {}, html_options = {})
  
在ActionView::Helpers::FormOptionsHelper中定義

如果資料來源是從資料庫來的話,可以使用這個來做下拉式選單。
這個Object不用我說,就是你的model
method呢?當然就是欄位啦
其實嚴格說起來,這只是select+options_from_collection_for_select的組合啦!
範例:

<%= collection_select(:payment, :id, @payments, :id, :name, 
  options ={:prompt => "-Select a continent"}, :class =>"payment") 
%

分享到:
评论

相关推荐

    jqueryAPI_helper

    jQuery(callback) 未翻译的翻译一下 slice(start,[end]) 未翻译的翻译一下 parent(expr) 例子中删除个body append(content) C要 ...submit(fn) select-&gt;submit toggle() 删除多余的 stop() 代码注释翻译

    Leopard hd install helper v0.3,硬盘助手3.0

    Step 2: Run "Leopard HD install helper" and select a ISO file. Step 3: Select target partition and Click "Start" Button. Step 4: When its done . reboot. Now , you can install leopard from HD.

    chara-ani AKB48 Select Helper-crx插件

    语言:日本語 这是一个扩展,可确保选择Chara-Ani的调度团队。 ...您可以重新排列每个成员的表(不是照片会话等事件)。...Comm网站。...此外,在V4.0.0,我们支持Stu 48剧院板现场。两者都没有足够的验证,所以如果它很有趣...

    SQLite.Helper:一个简单的工具,可帮助您使用C#管理SQLite数据库

    SQLite.Helper ####是否在C#项目中使用SQLite?####如果是,那就不用担心,SQLite.Helper是旨在帮助您管理SQLite数据库的类。... Get ( " SELECT `something` FROM `thattable` WHERE `otherthing` = 10

    .net数据访问类 SQL Helper 类

    SqlHelperParameterCache 类提供了三种可以用来管理参数的公共共享方法。它们是: CacheParameterSet。用于将 SqlParameters 数组存储到缓存中。 GetCachedParameterSet。用于检索缓存的参数数组的副本。 ...

    一步一步封装自己的HtmlHelper组件BootstrapHelper(三)

    前言:之前的两篇封装了一些基础的表单组件,这篇继续来封装几个...C#进阶系列——一步一步封装自己的HtmlHelper组件:BootstrapHelper(三:附源码) 一、NumberBoxExtensions NumberBoxExtensions是一个基于bootstra

    mybatis-helper:简单好用的mybatis扩展包,有助于减少XML配置文件,没有对mybatis源代码做任何修改或覆盖

    mybatis-helper简单介绍 1.基本用法 例如需要在controller中提供一个分页查询的API接口,代码可以这么写: @RequestMapping ( " /list " ) @ResponseBody public Object list( int pageNo, int pageSize, String ...

    NCKU-select-course-helper:成大选课小帮手

    NCKU选择课程助手一个开放源代码的网站,可从NCKU官方网站获取课程数据。 创建该网站的目的是为了帮助NCKU的学生轻松找到课程。 访问主要特点直观的用户界面更好的过滤功能以查找课程问题该应用无法自动定期更新课程...

    aliyun-tablestore-helper

    aliyun-tablestore-helper阿里云 tablestore 辅助方法const {TSHelper,TSSet,TSGet,TSRange,TSCreate} = require("aliyun-tablestore-helper");配置参数本地创建 ath.yaml ,内容:accessKeyId: 'xxxxxxxx'secret...

    ember-power-select-blockless:ember-power-select的无块版本

    该组件在内部使用ember-power-select,但使用get helper在列表的每个项目中呈现特定的属性。 所有选项与ember-power-select中的选项相同,但增加了一个新labelPath 。 这告诉组件要使用的选项中的哪个属性(以呈现...

    PHP数据库操作Helper类完整实例

    本文实例讲述了PHP数据库操作Helper类。...$db_select=mysql_select_db($db_database); 3. 执行CRUD操作 mysql_query("set names 'utf8'");//编码 $result=mysql_query($sqlstring); (mysql_affected_rows()

    android-sqlite-helper:适用于AndroidSDKSQLite助手

    实现非常简单的SELECT查询生成器 实施DDL查询构建器 实施迁移 引入CI(circleci或travis-ci或wrecker?) 撰写文件 创建示例应用程序 运送到MavenCentral 对于贡献者 使用AndroidStudio 1.0.0或更高版本进行...

    arnia-timesheet-helper

    安装golang cd arnia-timesheet-helper# replace with your Odoo project idgo run main.go -p=000快速CLI标志概述-p # project id, can be found under account_id/id in default Odoo exports-p=000 # select 000...

    ASP.NET MVC中HtmlHelper控件7个大类中各个控件使用详解

    HtmlHelper类在命令System.Web.Mvc.Html之中,主要由7个静态类组成,它们分别是FormExtensions类,InputExtensions类,LinkExtensions类,SelectExtensions类,TextExtensions类,ValidationExtensions类,...

    spree_variant_options_select

    SpreeVariantOptionsSelect 介绍在这里。 安装 将spree_variant_options_select添加到您的Gemfile中: ... 只需将此require语句添加到您的spec_helper中: require 'spree_variant_options_select/facto

    Endnote X8

    如果安装时出现了停滞在Installing Export Helper,则重新安装,custom installation」一步时,选择自定义安装「custom install],当进行到「Select Features」时,取消选择「Install Direct Helper」

    hexo-helper-quiz:帮助快速建立测验

    hexo-helper-测验一个有助于快速建立测验的插件。入门指南$ npm install hexo-helper-quiz用法{% quiz A Funny Test %}&lt;!-- Single choice question --&gt;...Select all books written by _Fushimi T

    SqlHelper MSSQL数据库辅助类 自己写的,不足之处请大家指教

    在需要用SqlHelper类的项目添加对System.Configuration的应用,有什么不足的地方希望大家...User user = helper.ExecuteObject("select * from users where name = @name", ps); 本辅助类没有对表存在判断的方法。。

    控制台:Symfony控制台组件的菜单帮助器

    RedAnt控制台 Symfony控制台的菜单选择帮助器 该菜单帮助程序是PHPSchool的库的桥梁,因此可以在Symfony控制台中轻松使用它。... use RedAnt \ Console \ Helper \ SelectHelper ; // Further on in your code ... $ t

    解决pycharm 远程调试 上传 helpers 卡住的问题

    今天小编就为大家分享一篇解决pycharm 远程调试 上传 helpers 卡住的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

Global site tag (gtag.js) - Google Analytics