æ¹è¯ä¸...å¿ã«åªããã¨ã©ã¼è¡¨ç¤ºãï¼
ç¾ç¶ã§ããããã¹ã¦ã®è¡ã§æ¤è¨¼ã¨ã©ã¼ãçºçããã¨ããã¨ããããªæãã«ãªã...ã
ãã¶ãæ´¾æã«æãã¦ããããä½å¦ãã©ããªã¨ã©ã¼ãªã®ãæå¿«ã¨è¨ãã°æå¿«ãªã®ã ã...ãã®è¡¨ç¤ºãä½åº¦ãè¦ã¦ãã¨ããªãã ãæ°æã¡ãéæ«ã§ããã¦ããããã§ããã¤ã®ã¾ã«ããè¨ãããªãã¦ãåãã£ã¨ãããï¼ãã¨ã ãã«ãªã£ã¦ããã®ã ãä½ã£ã¦ããæ¬äººããããªã®ã ãããã¦ã¼ã¶ã¼ããã®ã¨ã©ã¼ãè¦ããããããããããæ°ã失ããã ãã...ãããå°ããå¿ã«åªããã¨ã©ã¼è¡¨ç¤ºãå¿ è¦ã ã
é è²
赤éããã¨æçºçãªæãã«ãªã£ã¦ãã¾ããããå°ããã¼ã³ãæããè²ã«ãã¦ã¿ãã
- ã¨ã©ã¼æã®èæ¯è²ããã³ã¯ã«å¤æ´ããã
- paddingã0ã«ããã
- æ§ãç®ã«ãªãã®ã§ãinputãtextareaãselectã®èæ¯è²ããã³ã¯ã«ãã¦ã¿ãã
/* ã¹ã¿ã¤ã«ã·ã¼ã: public/stylesheets/scaffold.css */ ...ï¼ä¸ç¥ï¼... margin: 0; padding: 0; background-color: pink; display: table; } .fieldWithErrors * { background: pink; } ...ï¼ä¸ç¥ï¼...
é ç½®
ã¨ã©ã¼ãçºçããã¨ã大ããæ å ±éã§ããªãã®ã«ãã¹ã¯ãã¼ã«ããªãã¨ãã¼ã¸ãè¦æ¸¡ããªããªã£ã¦ãã¾ãã表示ãã³ã³ãã¯ãã«ã¾ã¨ããå¿ è¦ãããã
- ã¨ã©ã¼ã®çæ¿ãªã¹ã表示æ¹å¼ãããã¦ãåã ã®ãã£ã¼ã«ãã®ç´ä¸ã«ã¨ã©ã¼ã¡ãã»ã¼ã¸ã表示ããããã«ãã¦ã¿ãã
- error_message_onãå©ç¨ãã¦ãä¼ç¥¨ãæç´°è¡ã®_form.rhtmlã以ä¸ã®ããã«å¤æ´ãã¦ã¿ãã
- add_to_baseã使ã£ããã£ã¼ã«ããæå®ããªãæ¤è¨¼*1ã¯ã<%= error_message_on 'slip', 'base' %>ã§è¡¨ç¤ºãããã¨ãã§ãããï¼ï¼table class="journal"ï¼ã®ç´åã«è¿½å ãããï¼
<%# ãã¥ã¼: app/views/slips/_form.rhtml %> <%#= error_messages_for 'slip' %> <!--[form:slip]--> <p><label for="slip_number">ä¼ç¥¨No.</label><br /> <%= text_field 'slip', 'number', :autocomplete=>'off' %> <%= error_message_on 'slip', 'number' %> </p> <p><label for="slip_executed_on">å®è¡æ¥</label><br /> <%= text_field 'slip', 'executed_on', :autocomplete=>'off' %> <%= error_message_on 'slip', 'executed_on' %> </p> <p><label for="slip_total_yen">åè¨éé¡</label><br /> <%= yen_field 'slip', 'total_yen' %> <%= error_message_on 'slip', 'total_yen' %> </p> <%= error_message_on 'slip', 'base' %> <table class="journal"> <%= render :partial=>'journals/header' %> <%= render :partial=>'journals/form', :collection=>@journals %> </table> <!--[eoform:slip]-->
<%# ãã¥ã¼: app/views/slips/_form.rhtml %> <% @journal = form %> <% @journal_count += 1 rescue @journal_count = 1 %> <!-- åé¤ <tr> <td> </td> <td colspan="2"> <%#= error_messages_for 'journal' %> </td> </tr> ããã¾ã§åé¤ --> <!--[form:journal]--> <tr valign="top"> <th align="right"> <%= @journal_count %> <%= hidden_field "journal", 'position', :index=>@journal.index, :value=>@journal_count %> </th> <td align="_center"> <%= text_field "journal", 'comment', :index=>@journal.index, :size=>40 %> <%= error_message_on 'journal', 'comment' %> </td> <td align="_center"> <%= yen_field 'journal', 'yen', :index=>@journal.index %> <%= error_message_on 'journal', 'yen' %> </td> </tr> <!--[eoform:journal]-->
- ã¹ã¿ã¤ã«ã·ã¼ãã以ä¸ã®ããã«è¿½è¨ããã
/* ã¹ã¿ã¤ã«ã·ã¼ã: public/stylesheets/scaffold.css */ .formError { font-size: x-small; color: red; }
ããã¨ã以ä¸ã®ããã«ãå°ãã¯å¿ã«åªããæãã®è¡¨ç¤ºã«ãªãã
åé¡
ããããããã§åé¡çºçï¼æ°ã«ãªããã¨ã¯ä»¥ä¸ã®ï¼ç¹ã
- ãã£ã¼ã«ãã§è¤æ°ã®ã¨ã©ã¼ãçºçãã¦ããã¯ããªã®ã«ãä¸ã¤ã ãããã¨ã©ã¼è¡¨ç¤ºããã¦ããªãã
- ã¨ã©ã¼ã¡ãã»ã¼ã¸ã®ä¸»èªãçç¥ããã¦ãã¾ãã主èªããããã¨ãåæã¨ããã¡ãã»ã¼ã¸ãªã®ã§ããåè¨éé¡ã¨ä¸è´ãã¦ãã¾ããããã®ãããªããããªè¡¨ç¾ã«ãªã£ã¦ããã
- ã¨ã©ã¼ãçºçããã¨ãä¼ç¥¨No.ã»å®è¡æ¥ã»åè¨éé¡ã¨ãå ¥åãã£ã¼ã«ãã®éã«ä¸è¦ãªã¹ãã¼ã¹è¡ãå ¥ã£ã¦ãã¾ãããªãï¼
åé¡1: error_messages_onãå¿ è¦ï¼
Rails謹製ã®ã¡ã½ããã¯ä»¥ä¸ã®äºã¤ã
error_messages_for error_message_on
åã
ã®ãã£ã¼ã«ãç´ä¸ã«ã¨ã©ã¼ã¡ãã»ã¼ã¸ã表示ããçºã«error_message_onãå©ç¨ããããã©ããããã®ã¡ã½ããã¯ãæå®ãããã£ã¼ã«ãã§çºçãã¦ããã¨ã©ã¼ã®æåã®ä¸ã¤ã ããã表示ãã¦ãããªãä»æ§ã®ããã ã*2ããããè¦ãã¨ãerror_message(åæ°å½¢)_onã¨ãªã£ã¦ãããã¡ã½ããåçã«ã¯çãéã£ã¦ããã®ã...ã
ãããªãããã§ã¯ããã£ã¼ã«ããã¨ã«ãã¹ã¦ã®ã¨ã©ã¼ã表示ãã¦ãããerror_messages_onãå¿
è¦ã ãerror_message(åæ°å½¢)_onã®ã½ã¼ã¹ãåèã«ãã«ãã¼ã«ä»¥ä¸ã®ããã«æ¸ãã¦ã¿ãã
# ãã«ãã¼: app/helpers/application_helper.rb module ApplicationHelper ...ï¼ä¸ç¥ï¼... def error_messages_on(object, method, prepend_text = "", append_text = "", css_class = "formError") if (obj = (object.respond_to?(:errors) ? object : instance_variable_get("@#{object}"))) && (errors = obj.errors.on(method)) errors_list = errors.map {|error| "#{prepend_text}#{error}#{append_text}<br />"}.join content_tag("span", errors_list, :class => css_class) else '' end end end
ãã¹ã¦ã®error_message(åæ°å½¢)_onããæ°ããä½ã£ããã«ãã¡ã½ããerror_messages_onã«å¤æ´ãã¦ç¢ºèªããã¨...ã¨ã©ã¼ã¡ãã»ã¼ã¸ãè¤æ°è¡¨ç¤ºãããããã«ãªã£ãï¼
åé¡2: Ruby-GetTextãå©ç¨ãã
ã¨ã©ã¼ã¡ãã»ã¼ã¸ã®ä¸»èªãçç¥ãããã®ãerror_message_onï¼ã¾ãã¯error_messages_onï¼ã®ä»æ§ã ãããªãã·ã§ã³ã¨ãã¦prepend_textãè¨å®ã§ããããã«ãªã£ã¦ãããããã«ä¸»èªã«ãããæååãè¨å®ãã¦ããã°è¯ãã®ã ãã...åã
ã®error_messages_onã§ãã£ã¼ã«ãåãè¨å®ããã®ã¯é¢åã ãããã©ã«ãã§error_messages_forã®ããã«ä¸»èªããã®ã¨ã©ã¼ã¡ãã»ã¼ã¸ã«ãªãã°è¯ãã®ã«...ã
ãã®åé¡ã¯Ruby-GetTextãå©ç¨ãããã¨ã§è§£æ±ºãããæ¢ã«ã¤ã³ã¹ãã¼ã«ãã¦ããã°ãã決ã¾ãã®ã³ã¼ã2è¡ã¨ããªãªã¸ãã«ãªvalidateã®ã¨ã©ã¼ã¡ãã»ã¼ã¸ã«ã¯%{fn}ã追è¨ããã ãã§OKã
- Railsã§Ruby-GetTextãå©ç¨ãããã決ã¾ãã®ã³ã¼ã2è¡ã¯ä»¥ä¸ã®ããã«è¨å®ãï¼ãªã¬ã³ã¸è²ã®ç®æï¼
# è¨å®: config/environment.rb
...ï¼ä¸ç¥ï¼...
# Include your application configuration below
require 'gettext/rails'
# ã³ã³ããã¼ã©ã¼: app/controllers/application_controller.rb class ApplicationController < ActionController::Base ...ï¼ä¸ç¥ï¼... init_gettext 'test_slip' end
- environment.rbãå¤æ´ãããããµã¼ãã¼ã®åèµ·åãå¿ããã«ã
- validateã§ãªãªã¸ãã«ãªã¨ã©ã¼ã¡ãã»ã¼ã¸ãè¨å®ãã¦ããå ´åã¯ãæååä¸ã«%{fn}ã¨æ¸ãã¨ãRuby-GetTextãããããã£ã¼ã«ãåã«ç½®ãæãã¦ããããï¼ãªã¬ã³ã¸è²ã®ç®æï¼
# ã¢ãã«: app/models/slip.rb class Slip < ActiveRecord::Base has_many :journals, :order=>:position, :dependent=>:destroy validates_presence_of :number, :executed_on, :total_yen def validate # æç´°ã®å ¥åãã§ã㯠errors.add_to_base("æç´°ãä¸è¡ãå ¥åããã¦ãã¾ããã") unless journals_valid? # åè¨éé¡ã®ãã§ã㯠# nilãå«ã¾ããã¨æ°å¤ã¨ãã¦åãæ±ããªãã®ã§ãto_iã§æ°å¤ã«å¤æãã¦ããå¿ è¦ãã errors.add(:total_yen, "%{fn}ãæç´°ã®åè¨ã¨ä¸è´ãã¦ãã¾ããã") unless total_yen.to_i == journals_total_yen end ...ï¼ä¸ç¥ï¼...
class Journal < ActiveRecord::Base belongs_to :slip validates_presence_of :comment, :yen validates_exclusion_of :yen, :in=>(0..0), :message=>"%{fn}0 ã¯å ¥åã§ãã¾ããã" def validate errors.add(:yen, "%{fn}ãåè¨éé¡ã¨ä¸è´ãã¦ãã¾ããã") unless slip.total_yen.to_i == slip.journals_total_yen end ...ï¼ä¸ç¥ï¼...
- ã¤ãã§ã«ããã£ã¼ã«ãåã翻訳ãã¦ãããã
-
-
- Ruby-GetTextã®ã¡ãã£ã¨ãã使ãæ¹ã«ã¤ãã¦ã¯以前の日記:GetTextで日本語化してみる辺ããå¤å°ã¯åèã«ãªãããããã¾ãããï¼è©³ç´°ã¯Ruby-GetTextæ¬å®¶ã®ãã¼ã¸ã¸ï¼
-
ããã§ã¨ã©ã¼ã¡ãã»ã¼ã¸ãèªç¶ãªæ¥æ¬èªã«ãªã£ãï¼
åé¡3: fieldWithErrorsã®divã¿ã°ãspanã¿ã°ã«å¤æ´ãã
äºç´°ãªç©ºè¡ã®ãã¨ã ããçµæ§æ©ãã§ãã¾ã£ã...ãï¼scaffoldãçæããå ¥åãã©ã¼ã ã§ã¯ãã¹ã¦ãæ¤è¨¼ã¨ã©ã¼ãçºçããã¨ã©ãã«ã¨ãã©ã¼ã ã®éã«ä½åãªç©ºè¡ãå ¥ã£ã¦ãã¾ã...ãï¼
- scaffoldã¯ä»¥ä¸ã®ãããªå½¢å¼ã®å ¥åãã©ã¼ã ãçæãã¦ãããã
<!--[form:slip]--> <p><label for="slip_number">Number</label><br /> <%= text_field 'slip', 'number' %> </p> ...ï¼ä¸ç¥ï¼...
- ä¸è¨ã¯æ¤è¨¼ã¨ã©ã¼ãç¡ããã°ã以ä¸ã®htmlã«å¤æãããã
<!--[form:slip]--> <p><label for="slip_number">Number</label><br /> <input id="slip_number" name="slip[number]" size="30" type="text" /> </p>
- ããæ¤è¨¼ã¨ã©ã¼ãçºçããã°ã以ä¸ã®htmlãçæããããï¼ï¼div class="fieldWithErrors"ï¼ã¿ã°ã§å²ã¾ãããï¼
<!--[form:slip]--> <p><label for="slip_number">ä¼ç¥¨No.</label><br /> <div class="fieldWithErrors"><input id="slip_number" name="slip[number]" size="30" type="text" /></div> </p>
- é ã§èããã¨ãããã®ä½å¦ã«ç©ºè¡ãå ¥ãè¦å ãããã®ããå ¨ãç解ã§ããªãã£ã...ãï¼ããããã¦ï¼br /ï¼ã¿ã°ãæªããã¨æã£ãããã¦ãåé¤ãã¦ã¿ãããã¨ã©ã¼ç¡ãã®ç¶æ ã§ã¯ã©ãã«ã¨ãã©ã¼ã ãæ¹è¡ç¡ãã®ä¸è¡ã§è¡¨ç¤ºãããã¨ã©ã¼ãçºçããã¨ãã¯ãåãããã«ç©ºè¡ãå ¥ã£ãç¶æ ã«ãªã£ã¦ãã¾ããï¼
- ã¨ããããFirebugã§inspectãã¦ã¿ã¦æ°ä»ããï¼pã¿ã°ã®ä¸ã«ããã¯ãã®divã¿ã°ããpã¿ã°ã®å¤ã«å¼¾ãåºããã¦ãã¾ã£ã¦ãã...ã以ä¸ã®ãããªç¶æ³ã«ãªã£ã¦ããã
<p> <label for="slip_number">ä¼ç¥¨No.</label><br/> </p> <div class="fieldWithErrors"> <input id="slip_number" type="text" size="30" name="slip[number]" autocomplete="off"/> </div>
- ããã¯tableã¿ã°ã®ç´ä¸ã«divã¿ã°ãåå¨ããå ´åãåãããã«tableã¿ã°ã®å¤ã«åºããã¦è¡¨ç¤ºãããç¾è±¡ã¨åãã§ã¯ãªããï¼ã¨èãããpã¿ã°ã®ä¸ã«divã¿ã°ã®åå¨ã¯è¨±ãããªãã®ãããããªãã*3
- ããã§ãæ¤è¨¼ã¨ã©ã¼ãçºçããã¨ãï¼span class="fieldWithErrors"ï¼ã¿ã°ã§å²ãããã«å¤æ´ãã¦ã¿ãã*4environment.rbã§ä»¥ä¸ã®ããã«è¨å®ãããã¨ã§ããã¹ã¦ã®fieldWithErrorsã®ã¿ã°ãspanã¿ã°ã«å¤æ´ãããã
-
-
- rails:285 あべさんのスレッドã«æè¬ã§ãï¼
-
# è¨å®: config/environment.rb ...ï¼ä¸ç¥ï¼... # Include your application configuration below # fieldWithErrorsã®ã¿ã°ã<span>ã«å¤æ´ããã ActionView::Base.field_error_proc = Proc.new do |html_tag, instance| "<span class=\"fieldWithErrors\">#{html_tag}</span>" end
- environment.rbãå¤æ´ãããããµã¼ãã¼ã®åèµ·åãå¿ããã«ã
-
- ãªãä¸è¨ã³ã¼ãã§spanã¿ã°ã«ç½®ãæããããã®ããèªåã§ã¯ç解ã§ãã¦ããªã...ããããããæ¤è¨¼ã¨ã©ã¼çºçæã«ã©ã®ãããªä»çµã¿ã§fieldWithErrorsã¯ã©ã¹ã®ã¿ã°ãæ¿å ¥ãããã®ãããããããç解ã§ãã¦ããªããRailsã®ã½ã¼ã¹ã®ã©ã®é¨åãèªãã¹ããªã®ãï¼ç¥ããã...ã
-
以ä¸ã§ï¼ã¤ã®åé¡ã解決ã§ããï¼ãµã¼ãã¼ãåèµ·åãã¦è©¦ãã¦ã¿ãã
å¿ã«åªããã¨ã©ã¼è¡¨ç¤ºã«ãªã£ãã ãããï¼
*1:ä¾ï¼errors.add_to_base("æç´°ãä¸è¡ãå ¥åããã¦ãã¾ããã") unless journals_input?
*2:ããã¥ã¢ã«ã®show sourceã§ç¢ºèªããã¨ãã³ã¼ãã®ä¸ã«ãerrors.is_a?(Array) ? errors.first : errorsãã¨ãªã£ã¦ããç®æããããããã«ãã¦ããã©ããã¦ãããªä»æ§ã«ãªã£ã¦ãããã ããï¼
*3:htmlã§æ£ããã¯ãdivã¯è¤æ°ã®æ®µè½pãªã©ãããã¯ã¬ãã«è¦ç´ ãã°ã«ã¼ãåããããã«ä½¿ãããã ã
*4:spanã¯æ®µè½å ã®ç¹å®ã®æååãªã©ã¤ã³ã©ã¤ã³ã¬ãã«è¦ç´ ãã°ã«ã¼ãåããããã«ä½¿ãããã ã