form_forã§ãã©ã¼ã ãæ¸ã
ä¹ ãã¶ãã«Railsã触ãã!
ããã¼, ã¨ããææã«Railsã¯è§¦ã£ã¦ãããã©ã, æè¿ãã£ãã触ã£ã¦ããªãã®ã§, Railsã®é²åã«ã¨ã¾ã©ã£ã¦ãã次第ã§ã. ãªãã触ã£ã¦ããã®ã¯ãã¼ã¸ã§ã³1.2.3ã®ã ãã. ä»ã¯ãã2.2.2â¦â¦ ã¨ã¾ã©ãã®ã¯ä»æ¹ãªããã©, ã¡ããã¨ä½¿ã£ã¦ã¿ããã£ãã®ã§, ãªãããªãå ¼ãã¦è¿½ããããã³ãã«. ããã§ã³ã³ã«ã¡ã¢ã£ã¨ã.
ã²ã¨ã¾ãæ¬ã¯ä½¿ãã(ã¨ãããè²·ãã), ã½ã¼ã¹ã¨Webã§ãã£ã¦ãããã¨æã. ã¾ãã¯æ©éã¤ã¾ããããã©ã¼ã ã®æ¸ãæ¹.
form_forãã«ãã¼
Rails1.*ã®ã¨ãã¯start_form_tag, end_form_tag(form_tag)ã使ã£ã¦ãããã©, Rails2.0ããform_forã¨ãããã«ãã¼ã使ãããã«ãªã£ãããã. ããã«åããã¦, text_fieldãªã©ã®è¨è¿°æ¹æ³ãå¤æ´. 以ä¸ã®ããã«ãªã.
<% form_for :human, :url => {:action => 'create'} do |f| -%> <p> <label for="human_name">Name</label>: <%= f.text_field 'name' %> </p> <p> <label for="human_age">Age</label>: <%= f.text_field 'age' %> </p> <%= f.submit 'Save' %> <% end -%>
form_forã«ãã£ã¦(X)HTMLã®é層ã¨Rubyã®ãããã¯ã«ããã¤ã³ãã³ããããç¨åº¦ä¸è´ãã¦ããããããæã. ä»ã«ãfields_forãªã©ããã.
ãã¨ä»¥åã¯ä¸è¨ã®å ´åå¿ ã@humanå¤æ°ã«ã¢ãã«ã®ã¤ã³ã¹ã¿ã³ã¹ãå ¥ãã¦ããå¿ è¦ããã£ããã©, form_forã§ã¯ç¬¬2å¼æ°ã«ã¢ãã«ã®ã¤ã³ã¹ã¿ã³ã¹ãä¸ããã°ãã(ãã®å ´åãªãã·ã§ã³ã¯ç¬¬3å¼æ°)ã®ã§, å¿ ããã@humanã¨ããååã®å¤æ°ã§ããå¿ è¦ã¯ãªãã, ãã£ã¨è¨ãã°ä¸è¨ã®ããã«çç¥ã§ãã = newã®ã¨ãã«ã¢ãã«ã®ã¤ã³ã¹ã¿ã³ã¹ãçæããå¿ è¦ããªã.
ã¡ãªã¿ã«form_forã«æ¸¡ãã¦ãããããã¯ã®ãã©ã¡ã¼ã¿ã§ããfã«ã¯FormBuilderã¨ããã¯ã©ã¹ã®ã¤ã³ã¹ã¿ã³ã¹ã渡ããã模æ§. ã¨ããã³ãã§æ¬¡ã¯FormBuilder.
FormBuilder
ã³ã¬ã¯ç´ æµ. æãã. ã³ã¬ã使ãã¨f.text_fieldãªã©ã®æåãå¤ãããã¨ã.
ä»çµã¿ã¯åç´ã§, ã¾ãä¸è¨ã®ããã«FormBuilderã®ãµãã¯ã©ã¹ãä½ã.
# app/helpers/extended_form_builder.rb class ExtendedFormBuilder < ActionView::Helpers::FormBuilder def text_field(field, *args) (args[0] && args[0][:label] ? "#{label(field, args[0].delete(:label))}: " : '') + super end end
使ãã¨ãã¯ä¸è¨ã®ããã«ãã¦æå®ããã°OK.
<% form_for :human, :url => {:action => 'create'}, :builder => ExtendedFormBuilder do |f| -%> <%= f.text_field 'name', :label => 'Name' %> <% end -%>
ãããªé¢¨ã«åºã¾ã(text_fieldã®ã¨ããã ãæç²).
<label for="human_name">Name</label>: <input type="text" size="30" name="human[namae]" id="human_name"/>
ã£ã¦ãªå ·åã«ãã£ã¼ã«ãå¨ãã®ã¿ã°ãFormBuilderã®æ¹ã«åºãã¦ãã¾ãã³ããã§ããã®ãã¼.
å®éã«ã¯ä¸è¨ã®ããã«ä½¿ã£ã¦ã¿ã¾ãã(google:éåæã¢ãããã¼ããªãã¦ããé »ç¹ã«ä½¿ãã®ã, ã¨è¨ãããã°ã¢ã¬ã§ãã).
module ApplicationHelper def async_upload_form_for(record_or_name_or_array, *args, &proc) args << {} unless args[-1] && args[-1].class == Hash html_options = { :target => AsyncUploadFormBuilder.target(record_or_name_or_array), :multipart => true } options = args.pop options[:html] = (options[:html] || {}).update html_options options[:builder] = AsyncUploadFormBuilder form_for record_or_name_or_array, *(args << options), &proc end end
class AsyncUploadFormBuilder < ActionView::Helpers::FormBuilder def self.target(form_name) "#{form_name}_async_upload" end def file_field(field, *args) target = self.class.target(@object_name) options = { :id => target, :name => target, :style => 'width: 0; height: 0; position: absolute; left: -9999px;' } @template.content_tag(:iframe, '', options) + super end end
enctypeå±æ§ãtargetå±æ§ãåæã«è£å®ãã¦, file_fieldãå¼ã³åºããã¨ãã«iframeã¿ã°ãåºåãã, ã£ã¦ãªæãã§ã. å¾ã¯viewã«ä¸è¨ã®ãããªæãã§æ¸ãã ã.
<% async_upload_form_for :human, :url => {:action => 'upload'} do |f| -%> <%= f.file_field 'file' %> <%= f.submit 'Upload' %> <% end -%>
ãã£ãã!