- 在model中如果有个字段是 default_contact ,这个字段在编辑的时候要用radio方式来让用户选择。那么就要用到下面的代码:
<%= f.input :default_contact, :as => :radio, :collection => [['默认联系人', '1'], ['非默认联系人', '0']], :wrapper_html => { :class => :'field-toggle-buttons' }, :boolean_style => :inline %>
- 在form中进行列表显示,显示的时候要定义各个列的宽度:
在/assets/javascripts/controllers/模块名/list.js.coffee 文件中进行定义,相应的语句是:
aoColumns = ({"mDataProp": $(th).attr('data-prop')} for th in $('#entities-list > thead > tr > th') when $(th).attr('data-prop') != undefined ) oTable = $('#entities-list').dataTable($.extend(global_datatable_options, "bAutoWidth": false "aoColumnDefs":[ {"aTargets": [0], "sType": "numeric", "sWidth": "55px" } {"aTargets": [1], "sWidth": "" } {"aTargets": [2], "sWidth": "280px" } {"aTargets": [3], "sWidth": "280px" } {"aTargets": [-1], "sWidth": "70px", "bSearchable": false, "bSortable": false } {"aTargets": ["_all"], "sWidth": "90px" } ]
其中aoColumnDefs
数组代码各个列的表现形式参数。sWidth 来控制宽度。如果不写 代表自动计算。数组的下标代表第几个列。 [-1] 代表从右往左 计算。 ["_all"] 代表缺省的
- 在rails中实现表和表,一对多关联的时候,使用下拉框来选择内容的功能
有个分支机构代码表 branch , 一个仓库表 warehouse . 其中branch中有个字段 warehouse_code 要对应 warehouse表中的code字段。并且在添加和编辑 branch 内容的时候,要通过下拉选择框来选取warehouse。
处理的方法:
I. 在branch的model中增加一个关联 ,取名叫 warehouse ,并且指定 外键
belongs_to :warehouse, :foreign_key => :storage_code ,:primary_key => :code
II. 在warehouse的model中定义一个用于显示 下拉框内容的 方法
def display_name "#{code} | #{name}" end
III. 在form.html中 使用 下拉框代码
<%= f.association :warehouse, :input_html => { :class => 'span2 chzn-select'}, :label_method => :display_name, :value_method => :code %>
其中 f.association :warehouse 就是要和 branch model文件中的 外键定义 一致