simple_form ãTable è¦ç´ ã§ä½¿ã
simple_form ã¯ãRails ç¨ã®form_for ã®æ¡å¼µã¿ãããªãã®ã§ãã
æ®éãform_for ã使ãã¨ãã¿ã°çã«ã¤ãã¦èªåã§æ¸ãã¦ãããªãã¨ããã¾ããããsimple_form ãä¸æãéç¨ããã¨ãviewå
ã«ã¯form ã«é¢é£ããã¿ã°ãæ¸ãå¿
è¦ã¯ããã¾ããï¼çãªæãï¼
éã«ããã¨ãã¿ã°ãåæã«æ¸ãã¦ããã®ã§ããã¶ã¤ãã¼ã¸ã®æ®ºå·è½åã¯é«ãæ°ããã¾ããä¸æãã³ã³ããã¼ã«ããã®ã大å¤ã§ã(CoCã®å¼å®³ã ã¨æã)ã
simple_form ã¯ãdivè¦ç´ ã§ã®éç¨ãããã©ã«ãªã£ã¦ã¾ãã
ã§ã表é¡ã®ãããªãã£ã¬ã³ã¸ã§ããããã¦tableã§ææ¢ã«ææ¦ãã¾ãã
ã¾ããviewã®æ¹ã«ã¯ã以ä¸ã®ããã«æ¸ãã¾ãã
<%= simple_form_for @user do |f| %> <table> <%= f.input :name %> <%= f.input :email %> </table> <% end %>
ã¯ããã·ã³ãã«ã§ãããããããã®ã¾ã¾ã ã¨divãã¾ãã
次ã«ãçæããformç¨ã®htmlãå¤æ´ããããã«ãwrapper ã¨ãããã®ãå¤æ´ãã¾ããwrapperã¯config/initializers/simple_form.rb ã§å®ç¾©ãã¾ãã
ä»åã¯ãããã©ã«ãã®wrapperãå¤æ´ãã¾ãã
çµè«ã§ããã以ä¸ã®ããã«ç·¨éãã¦ãã ããã
config.wrappers :default, :tag => :tr, :class => :input, :hint_class => :field_with_hint, :error_class => :field_with_errors do |b| #ããã©ã«ãã®ã³ã¼ããæ¸ãã¦ããã¾ãã ## Inputs b.use :label, :wrap_with => {:tag => :th} # or :td, you prefer b.use :input, :wrap_with => {:tag => :td} b.use :hint, :wrap_with => { :tag => :span, :class => :hint } b.use :error, :wrap_with => { :tag => :span, :class => :error } end
wrapper ãä½ãwrapãããã¨ããã¨ãf.input ãæå³ãããã®ã§ãã
ä¸ããï¼ï¼ï¼è¡ç®ã§ããf.inputã¯labelã¨inputãæç»ãã¾ããã¨ãã宣è¨ããã¦ãã¾ãã
ãã®f.inputãwrapããããã®è¨å®ããconfig.wrappersã®å¼æ°ã§ãtrè¦ç´ (表ã®è¡)ã«ãã£ã¦å²ã£ã¦ãã¾ãã
è¡ã®ä¸ã«ãlabelã¨inputãåºæ¥ãããã§ããããããããtdè¦ç´ ã§å²ããã¨ã«ãã£ã¦ãlabelç¨ã®åã¨inputç¨ã®åãã§ãã¾ãã
æçµçã«ããããªæ§é ã®htmlãã§ãã¾ã(ããªãçãã¦ã¾ãã)
<form> <table> <tr> <th>åå</th> <td><input name="name"></td> </tr> <tr> <th>ã¡ã¼ã«ã¢ãã¬ã¹</th> <td><input name="email"></td> </tr> </table> </form>
ææ³
ããã®ä½ããããªããã¨ããã¨ãviewå
ã«ããã¦ãçæãããhtmlã®ç¹æ§ãç¥ããªãã¨ãtableå¼ã®formãä½ã£ã¦ãããªããã¨ãããã¨ã§ãã
ããå°ãå
·ä½çã«è¨ãã¨ãerbå
ã§ã¯ãsimple_form_forã¨f.inputã ãã§ãªããtableè¦ç´ ã«ã¤ãã¦æ¸ããªããã°ãªãã¾ããã
ãããªã®ç³ã§ãã