- 浏览: 243371 次
- 性别:
- 来自: 杭州
文章分类
- 全部博客 (173)
- ruby (38)
- rails (42)
- javascript (7)
- jquery (1)
- linux (15)
- design patterns (1)
- project management (6)
- IT (7)
- life (19)
- data structures and algorithm analysis (2)
- css (1)
- prototype (1)
- mysql (4)
- html (1)
- git (3)
- novels (1)
- c (1)
- Latex (13)
- erlang (1)
- 求职 (1)
- API (0)
- Shell (4)
- Rabbit MQ (1)
- 计算机基础 (1)
- svn (2)
- 疑问 (1)
最新评论
-
zhangyou1010:
回去倒立去,哈哈。
作为一个程序员,身体很重要! -
Hooopo:
Ruby MetaProgramming is all abo ...
Metaprogramming Ruby -
orcl_zhang:
yiqi1943 写道LZ现在上学还是工作呢工作好多年了。不过 ...
2011年 -
yiqi1943:
LZ现在上学还是工作呢
2011年 -
tjcjc:
query cache
就是一个简单的hash
key就是sq ...
Rails sql延迟加载和自带缓存
阅读api是一种乐趣!
api of fields_for===========
fields_for(record_or_name_or_array, *args) {|builder.new(object_name, object, self, options, block)| ...}
Creates a scope around a specific model object like form_for, but doesn‘t create the form tags themselves. This makes fields_for suitable for specifying additional model objects in the same form.
Generic Examples
…or if you have an object that needs to be represented as a different parameter, like a Client that acts as a Person:
…or if you don‘t have an object, just a name of the parameter:
Note: This also works for the methods in FormOptionHelper and DateHelper that are designed to work with an object as base, like FormOptionHelper#collection_select and DateHelper#datetime_select.
Nested Attributes Examples
When the object belonging to the current scope has a nested attribute writer for a certain attribute, fields_for will yield a new scope for that attribute. This allows you to create forms that set or change the attributes of a parent object and its associations in one go.
Nested attribute writers are normal setter methods named after an association. The most common way of defining these writers is either with accepts_nested_attributes_for in a model definition or by defining a method with the proper name. For example: the attribute writer for the association :address is called address_attributes=.
Whether a one-to-one or one-to-many style form builder will be yielded depends on whether the normal reader method returns a single object or an array of objects.
One-to-one
Consider a Person class which returns a single Address from the address reader method and responds to the address_attributes= writer method:
This model can now be used with a nested fields_for, like so:
When address is already an association on a Person you can use accepts_nested_attributes_for to define the writer method for you:
If you want to destroy the associated model through the form, you have to enable it first using the :allow_destroy option for accepts_nested_attributes_for:
Now, when you use a form element with the _delete parameter, with a value that evaluates to true, you will destroy the associated model (eg. 1, ‘1’, true, or ‘true’):
One-to-many
Consider a Person class which returns an array of Project instances from the projects reader method and responds to the projects_attributes= writer method:
This model can now be used with a nested fields_for. The block given to the nested fields_for call will be repeated for each instance in the collection:
It‘s also possible to specify the instance to be used:
When projects is already an association on Person you can use accepts_nested_attributes_for to define the writer method for you:
If you want to destroy any of the associated models through the form, you have to enable it first using the :allow_destroy option for accepts_nested_attributes_for:
This will allow you to specify which models to destroy in the attributes hash by adding a form element for the _delete parameter with a value that evaluates to true (eg. 1, ‘1’, true, or ‘true’):
api of fields_for===========
fields_for(record_or_name_or_array, *args) {|builder.new(object_name, object, self, options, block)| ...}
Creates a scope around a specific model object like form_for, but doesn‘t create the form tags themselves. This makes fields_for suitable for specifying additional model objects in the same form.
Generic Examples
<% form_for @person, :url => { :action => "update" } do |person_form| %> First name: <%= person_form.text_field :first_name %> Last name : <%= person_form.text_field :last_name %> <% fields_for @person.permission do |permission_fields| %> Admin? : <%= permission_fields.check_box :admin %> <% end %> <% end %>
…or if you have an object that needs to be represented as a different parameter, like a Client that acts as a Person:
<% fields_for :person, @client do |permission_fields| %> Admin?: <%= permission_fields.check_box :admin %> <% end %>
…or if you don‘t have an object, just a name of the parameter:
<% fields_for :person do |permission_fields| %> Admin?: <%= permission_fields.check_box :admin %> <% end %>
Note: This also works for the methods in FormOptionHelper and DateHelper that are designed to work with an object as base, like FormOptionHelper#collection_select and DateHelper#datetime_select.
Nested Attributes Examples
When the object belonging to the current scope has a nested attribute writer for a certain attribute, fields_for will yield a new scope for that attribute. This allows you to create forms that set or change the attributes of a parent object and its associations in one go.
Nested attribute writers are normal setter methods named after an association. The most common way of defining these writers is either with accepts_nested_attributes_for in a model definition or by defining a method with the proper name. For example: the attribute writer for the association :address is called address_attributes=.
Whether a one-to-one or one-to-many style form builder will be yielded depends on whether the normal reader method returns a single object or an array of objects.
One-to-one
Consider a Person class which returns a single Address from the address reader method and responds to the address_attributes= writer method:
class Person def address @address end def address_attributes=(attributes) # Process the attributes hash end end
This model can now be used with a nested fields_for, like so:
<% form_for @person, :url => { :action => "update" } do |person_form| %> ... <% person_form.fields_for :address do |address_fields| %> Street : <%= address_fields.text_field :street %> Zip code: <%= address_fields.text_field :zip_code %> <% end %> <% end %>
When address is already an association on a Person you can use accepts_nested_attributes_for to define the writer method for you:
class Person < ActiveRecord::Base has_one :address accepts_nested_attributes_for :address end
If you want to destroy the associated model through the form, you have to enable it first using the :allow_destroy option for accepts_nested_attributes_for:
class Person < ActiveRecord::Base has_one :address accepts_nested_attributes_for :address, :allow_destroy => true end
Now, when you use a form element with the _delete parameter, with a value that evaluates to true, you will destroy the associated model (eg. 1, ‘1’, true, or ‘true’):
<% form_for @person, :url => { :action => "update" } do |person_form| %> ... <% person_form.fields_for :address do |address_fields| %> ... Delete: <%= address_fields.check_box :_delete %> <% end %> <% end %>
One-to-many
Consider a Person class which returns an array of Project instances from the projects reader method and responds to the projects_attributes= writer method:
class Person def projects [@project1, @project2] end def projects_attributes=(attributes) # Process the attributes hash end end
This model can now be used with a nested fields_for. The block given to the nested fields_for call will be repeated for each instance in the collection:
<% form_for @person, :url => { :action => "update" } do |person_form| %> ... <% person_form.fields_for :projects do |project_fields| %> <% if project_fields.object.active? %> Name: <%= project_fields.text_field :name %> <% end %> <% end %> <% end %>
It‘s also possible to specify the instance to be used:
<% form_for @person, :url => { :action => "update" } do |person_form| %> ... <% @person.projects.each do |project| %> <% if project.active? %> <% person_form.fields_for :projects, project do |project_fields| %> Name: <%= project_fields.text_field :name %> <% end %> <% end %> <% end %> <% end %>
When projects is already an association on Person you can use accepts_nested_attributes_for to define the writer method for you:
class Person < ActiveRecord::Base has_many :projects accepts_nested_attributes_for :projects end
If you want to destroy any of the associated models through the form, you have to enable it first using the :allow_destroy option for accepts_nested_attributes_for:
class Person < ActiveRecord::Base has_many :projects accepts_nested_attributes_for :projects, :allow_destroy => true end
This will allow you to specify which models to destroy in the attributes hash by adding a form element for the _delete parameter with a value that evaluates to true (eg. 1, ‘1’, true, or ‘true’):
<% form_for @person, :url => { :action => "update" } do |person_form| %> ... <% person_form.fields_for :projects do |project_fields| %> Delete: <%= project_fields.check_box :_delete %> <% end %> <% end %>
# File vendor/rails/actionpack/lib/action_view/helpers/form_helper.rb, line 476 476: def fields_for(record_or_name_or_array, *args, &block) 477: raise ArgumentError, "Missing block" unless block_given? 478: options = args.extract_options! 479: 480: case record_or_name_or_array 481: when String, Symbol 482: object_name = record_or_name_or_array 483: object = args.first 484: else 485: object = record_or_name_or_array 486: object_name = ActionController::RecordIdentifier.singular_class_name(object) 487: end 488: 489: builder = options[:builder] || ActionView::Base.default_form_builder 490: yield builder.new(object_name, object, self, options, block) 491: end
发表评论
-
calendar
2012-02-24 11:04 877http://fullcalendar.vinsol.com/ ... -
ActiveRecord::Dirty
2011-11-21 10:29 789引用Track unsaved attribute chang ... -
TinyTDS
2011-09-20 09:29 862tiny_tds https://github.com/ra ... -
pandoc-ruby
2011-09-11 11:50 1211https://github.com/alphabetum/p ... -
Rails: Calling render() outside your Controllers
2011-04-28 17:15 835From:http://blog.choonkeat.com/ ... -
为什么这样才能装上
2011-02-20 10:39 1046引用u2@u2-laptop:~$ sudo gem inst ... -
Rails的transaction
2011-01-07 18:36 3059今天同事问我关于rails transaction,如 ... -
Rails sql延迟加载和自带缓存
2010-12-30 01:11 1612color_lot_manuallies = color_lo ... -
关于rhtml
2010-12-23 00:26 871在视图里有这样一段代码 sorted_op_items = o ... -
will_paginate ajax
2010-11-26 13:21 913两种方法 一, @@pagination_options ... -
save > save!(转)
2010-11-19 19:57 757Thoughtbot folks have a great a ... -
USE INDEX with Active Record finders(转)
2010-11-18 22:07 894可以通过强制指定index的方法优化find MySQL do ... -
html转义
2010-11-17 23:03 955$("#contacts").html(& ... -
Rails HTTP Status Code to Symbol Mapping
2010-11-17 22:40 1623http状态码http://zh.wikipedia.org/ ... -
Scaling Rails很不错的视频
2010-09-29 18:10 824自从railscasts开始讲解rails3后就很久没看了。 ... -
ActionController源码(待续)
2010-09-20 15:14 1027/usr/local/lib/ruby/gems/1.8/ge ... -
rails源码ActionSupport(待续)
2010-08-31 16:59 935一些奇淫技巧 class Object # An ... -
动态的增加auto_complete
2010-08-30 12:17 896http://www.iteye.com/problems/3 ... -
rails 记录
2010-08-26 15:27 751代码里有这样一句 self.purchase_invoices ... -
用Array来实现OrderedHash
2010-08-18 14:29 916偶然发现电脑的角落里有这样的一段代码.功能是用Array实现的 ...
相关推荐
`formfield_for_dbfield`函数正是这样一个工具,它允许我们在Django的Admin模型中自定义字段的表单字段。本实例将详细讲解如何利用`formfield_for_dbfield`实现过滤下拉表单的选择项。 首先,确保你的环境是Django ...
Receptive_Field_Block_Net_for_Accurate_and_Fast_Ob_RFBNet
field oriented control for induction motor
标题中的"Matlab code for phase field.zip"是一个包含Matlab代码的压缩文件,主要用于实现相场法(Phase Field)的模拟。相场法是一种广泛应用于材料科学、固体力学、流体动力学等领域的数值计算方法,它通过引入...
火炬接受野 在pytorch中计算CNN接收字段的大小 用法 git clone ...receptive_field_for_unit ( receptive_field_dict , "2" , ( 2 , 2 ))
the program is free, so every one can used it for ultrosound sitimulate , it is very useful.fieldii is used for field ii,i hope you will like it .
this is the vlsi based project that is implimentation of reconfigurable multipliers for integers and galois field multiplication
1. `xdc_convex_focused_multirow.m`:这可能是一个用于仿真凸形聚焦多排探头的函数,其中"xdc"可能代表"超声波传播的离散差分"(Discrete Difference Calculations for Ultrasonic Waves Propagation),"convex_...
no_help_for_field = 2 inconsistent_help = 3 no_values_found = 4 others = 5. ``` 此外,你还可以指定回调程序和形式来处理F4窗口返回的结果。 3. **F4_FILENAME** F4_FILENAME函数主要用来提供文件名的...
"Phase Field Modeling of Solidification Microstructural Evolution for.pdf"这篇文档很可能是详细阐述这一主题的专业论文。其中可能包含以下内容:首先,会介绍相场模型的基本理论,包括基本方程的建立,如Cahn-...
描述中的“for markov random field”进一步确认了这是关于如何使用MCMC技术处理马尔科夫随机场的资源集合。 马尔科夫随机场是统计物理和概率论中的一个概念,它用于描述系统中各状态之间的依赖关系,其中每个状态...
return super(ColorStyleAdmin, self).formfield_for_dbfield(db_field, **kwargs) admin.site.register(ColorStyle, ColorStyleAdmin) ``` 5. **jscolor库**: jscolor是一个轻量级的颜色选择器库,仅包含一个...
基于人工势场的岔路口的道路建模研究(程序)
该代码是对文章A Modified Fuzzy C-Means Algorithm for Bias Field Estimation and Segmentation of MRI Data算法的实现
After that, I describe for you the case study that is the framework for the book. This includes background of FutureTech, Inc., the network layout that the company has, and the technologies you are ...
在"users’ guide for the field ii program.pdf"这份文档中,用户可以找到关于如何安装、配置和使用Field II程序的详细步骤。文档通常会包含以下几个部分: 1. **安装与启动**:介绍如何下载和安装Field II软件,...
这个压缩包文件"vray2.0_sp1_for_3dmax9.0chinese32bit.zip"包含了中文版的V-Ray 2.0 SP1,意味着它已经本地化,方便中国用户理解和操作。 V-Ray的核心功能在于其先进的光线追踪和全局光照技术,能够模拟真实世界的...
1992), for the improvement of radiative-transfer codes, especially to account for rain and hail in the microwave range and for aerosols and clouds in the submillimeter, infrared and visible range. ...
在"Phase Field Simulation for fracture_phasefieldmodel_裂缝_裂缝预测_phasefield_断裂相场_源码.rar"这个压缩包中,包含了关于相场模型用于预测裂缝的研究资料。源码部分可能涵盖了相场方程的离散化、求解算法...