Header

  1. View current page

    RubyOnRails 2.1 - What's New!

06. 액션뷰

ActionView

액션뷰는 ERB 템플릿을 이용하여 사용자에게 보여지는 화면 인터페이스를 생성하는 레이어이다.

ActionView::Helpers::FormHelper

fields_for form_for with index option.

 #fields_forform_for 메소드에서 각 form 항목에 :index => nil  를 지정하지 않아도  :index 옵션을 사용할 수 있다.

 

예전의 구현 예:

  1. <% fields_for "project[task_attributes][]", task do |f| %>
    <%= f.text_field :name, :index => nil %>
    <%= f.hidden_field :id, :index => nil %>
    <%= f.hidden_field :should_destroy, :index => nil %>
    <% end %>

새로운 구현 예:

  1. <% fields_for "project[task_attributes][]", task,
    :index => nil do |f| %>
    <%= f.text_field :name %>
    <%= f.hidden_field :id %>
    <%= f.hidden_field :should_destroy %>
    <% end %>

ActionView::Helpers::DateHelper

Now, all these module methods dealing with dates (date_select, time_select, select_datetime, etc.) are accepting HTML options. Look an example using date_select

날짜, 시간을 다루는 모든 메소드에서(date_select, time_select, select_datetime, 등) HTML 의 옵션들을 사용할 수 있다. date_select 를 사용하는 예제는 다음과 같다.

  1. <%= date_select 'item','happening', :order => [:day], :class => 'foobar'%>

date_helper

 date_helper 메소드의 기본값은 Date.current 으로 정해진다.

 

 

ActionView::Helpers::AssetTagHelper

register_javascript_expansion

이 메소드는 하나 이상의 자바스크립트 파일들을 특정 심볼에 대해서 등록할 수 있게 해준다. 이렇게 등록한 심볼을  javascript_include_tag 의 파라미터로 지정하면 연결된 자바스크립트 파일들이 모두 include된다.  예를들어  여러분이 만드는 플러그인의 init.rb 에서 이 메소드를 사용하여, 그 플러그인에서 사용하는 자바스크립트 파일들을 지정해둘 수 있다. 

사용 예제:

 

  1. # In the init.rb file
    ActionView::Helpers::AssetTagHelper.register_javascript_expansion
    :monkey => ["head", "body", "tail"]

    # In our view:
    javascript_include_tag :monkey

    # We are going to have:
    <script type="text/javascript" src="/javascripts/head.js"></script>
    <script type="text/javascript" src="/javascripts/body.js"></script>
    <script type="text/javascript" src="/javascripts/tail.js"></script>

register_stylesheet_expansion

바로 위에서 설명한 ActionView::Helpers::AssetTagHelper#register_javascript_expansion 메소드와 동일한 방식으로 CSS stylesheet 파일들을 하나의 심볼에 대해 지정하고,  stylesheet_link_tag 에서 사용한다. 예제는 다음과 같다.

  1. # In the init.rb file
    ActionView::Helpers::AssetTagHelper.register_stylesheet_expansion
    :monkey => ["head", "body", "tail"]

    # In our view:
    stylesheet_link_tag :monkey

    # We are going to have:
    <link href="/stylesheets/head.css" media="screen" rel="stylesheet"
    type="text/css" />
    <link href="/stylesheets/body.css" media="screen" rel="stylesheet"
    type="text/css" />
    <link href="/stylesheets/tail.css" media="screen" rel="stylesheet"
    type="text/css" />

ActionView::Helpers::FormTagHelper

submit_tag

:confirm 옵션이 추가되었다. 이 옵션은 link_to에서 사용될 때와 같은 방식으로 동작한다.

사용 예:

  1. submit_tag('Save changes', :confirm => "Are you sure?")

ActionView::Helpers::NumberHelper

number_to_currency

The number_to_currency method now accepts the :format option as a parameter, letting us to format the method's returning value. In earlier versions, when we had to format values for our local currency, we needed to include a space in front of :unit option to make the output format correct. See the examples:

:format 옵션이 추가되었다.  이 옵션을 사용해서 리턴 결과의 포맷을 지정할 수 있다. 이 전 버전에서는 각 국가별 통화 포맷팅을 제대로 하기 위해서서 :unit 옵션의 값 처음에  공백문제를 추가했어야 했다.  새로운 사용 예는 아래와 같다.

  1. # R$ is the symbol for Brazilian currency
    number_to_currency(9.99, :separator => ",", :delimiter => ".", :unit => "R$")
    # => "R$9,99″

    number_to_currency(9.99, :format => "%u %n", :separator => ",",
    :delimiter => ".", :unit => "R$")
    # => "R$ 9,99″

또 다음과 같이 새로운 포맷을 정의할 수도 있다.

  1. number_to_currency(9.99, :format => "%n in Brazilian reais", :separator => ",",
    :delimiter => ".", :unit => "R$")
    # => "9,99 em reais"

포맷팅 문자열에서 다음의 특수 부호가 사용된다.

  1. %u For the currency
    %n For the number

ActionView::Helpers::TextHelper

excerpt

excerpt 메소드는 헬퍼 메소드의 하니로 문장내애서 지정된 부분을 찾은 뒤, 지정된 갯 수만큼의  앞 뒤  글자들을 포함하여 '...'으로 감싸서 돌려준다. 다음의 예를 보면 이해하기 쉽다.

  1. excerpt('This is an example', 'an', 5)
    # => "…s is an examp…"

그런데 기존에는 앞 뒤의 문자 수를 지정한 것과 다르게 포함하는 버그가 있었는데(위의 예를 보면 뒷 부분에 5자가 아니라 6자가 추가되었다.) 이 것이 수정되었다. 수정된 후의 결과는 아래와 같다.

  1. excerpt('This is an example', 'an', 5)
    # => "…s is an exam…"

simple_format

이 메소드는 원래  임의의 문자열을 입력으로 받아서 단순한 방식으로 HTML로 포맷팅한다. 즉 줄바꿈을 "< br />" 로 바꾸고  줄바꿈이 두 번 연속 나오면 "< p>"로 변경한다.

레일스 2.1이 되면서 이 메소드는 "<p >" 태그에 적용할 HTML 속성값(class 등)을 추가 옵션으로 지정할 수 있게 되었다. 다음의 예와 같다.

  1. simple_format("Hello Mom!", :class => 'description')
    # => "<p class=’description’>Hello Mom!</p>"

 

 

History

Last edited on 06/18/2008 17:55 by ikspres

Comments (0)

You must log in to leave a comment. Please sign in.