æ¹è¯ä¸...yen_fieldã欲ããï¼
æ¥åã·ã¹ãã ã®å ¥åã«ã¯éé¡ãã¤ããã®ã ãã§ãç¾ç¶ã§ã¯ããã®åº¦ã«ä»¥ä¸ã®ããã«æ¸ãå¿ è¦ãããã
<%= text_field 'slip', 'total_yen', :value=>number_with_delimiter(@slip.total_yen), :autocomplete=>'off', :style=>"text-align:right" %>
ä¸è¨ã®ãããªé·ãé·ããªãã·ã§ã³ã¨å ±ã«ä½åº¦ãæ¸ãã®ã¯é¢åã ...ãã§ãããã¨ãªããyen_field 'slip', 'total_yen'ãã§æ¸ã¾ããããããã§ã½ã¼ã¹ãè¦ã¦æ©ãã çµæã以ä¸ã®ããã«ãã«ãã¼ã¨ãã¦ç»é²ãã¦ããã°ãä½ã¨ãå®ç¾ã§ãããã
- options.merge!ã§ããã©ã«ãã®ãªãã·ã§ã³ãè¨å®ãã¦ãããã¨ã§ãèªå好ã¿ã®ä¾¿å©ãªãã©ã¼ã ã«èªç±ã«å¤æ´ãããã¨ãã§ããã
# ãã«ãã¼: app/helpers/application_helper.rb module ApplicationHelper def yen_field(object_name, method, options = {}) # object_nameã«åºã¥ããªãã¸ã§ã¯ã(ã¢ãã«ã®ã¤ã³ã¹ã¿ã³ã¹)ãããmethodã示ããã£ã¼ã«ãã®å¤ãåå¾ãã¦ããã # ä¾: yen_field 'slip', 'total_yen' --> @slip.total_yen ã value ã«è¨å®ãããã object = self.instance_variable_get("@#{object_name}") value = object.send(method) # ããã©ã«ãã®ãªãã·ã§ã³è¨å® options.merge!(:value=>number_with_delimiter(value), :autocomplete=>'off', :style=>"text-align:right") ActionView::Helpers::InstanceTag.new(object_name, method, self, nil, options.delete(:object)).to_input_field_tag("text", options) end end
ããã§ä»¥ä¸ã®ããã«æ¸ããããã«ãªã£ãï¼ã·ã³ãã«ã§è¦ããããªã£ãï¼
<%= yen_field 'slip', 'total_yen' %>
å¿ããã¡ãªãã¨
ãã®yen_fieldã¯ã«ã³ãåºåãã®æ°æååããã®ã¾ã¾ãã©ã¡ã¼ã¿ã¼ã¨ãã¦éä¿¡ããã®ã§ãåä¿¡å´ã®slipã¢ãã«ã§ã¯ä»¥ä¸ã®ããã«ãã¦ãããªãã¨ï¼æååããã«ã³ããé¤å¤ããï¼ãããããªæ°åãåãåããã¨ã«ãªãã
def total_yen=(value) self[:total_yen] = value.gsub(/,/, '') end
-
-
- ãã®é¢åãããã«ã³ãã®é¤å¤æé ãç¡ããæ¹æ³ã£ã¦ããã ãããï¼
-