액션 뷰(폼 도우미 메소드)

IT(Old)/RubyOnRails-Tip 2008. 1. 22. 12:19
<% form_for :person, Lurl => {:action => "create"} do |f| %>
<p>이름: <%= f.text_field("name") %> </p>
<p>그룹: <%= f.select("group",
                                 [["A", "1"], ["B", "2"], ["C", "3"]]) %> </p>
<p>노트: <br /><%= f.text_area("note") %> </p>
<p><%= submit_tag("입력") %></p>
<% end %>

f.text_field(name, options) : 텍스트 필드를 출력해줌. 한줄의 텍스트를 입력받을 때 사용
f.text_area(name, options): TextArea 필드를 출력해줌
f.password_field(name, options): 패스워드 필드를 출력해줌.
f.hidden_field(name, options): 히든 필드를 출력해줌
f.select(name, options):콤보박스 필드를 출력해줌.
<%= f.select("group_id", @groups.map{|g| [g.name, g.id]},
                   :selected => "2", :include_blank => true) %>
=>
<select name="person[group_id]">
...

f.date_select(name, options): 날짜 입력을 위한 콤보 박스 필드를 출력해줌.
<% f.date_select("birth_date", :start_year => 2000,
                                           :end_year => 2010,
                                           :use_month_numbers => true) %>

f.datetime_select(name, options): 날짜 입력을 위한 콤보 박스 필드를 출력해 줌
f.check_box(name, options): 체크박스 필드를 출력해줌
f.radio_button(name, options): 라디오 버튼 필드를 출력해줌
f.file_field(name, options): 파일 업로드 필드를 출력해줌
submit_tag(name): html 입력 필드를 출력해줌

액션뷰 (기본도우미 메소드)

IT(Old)/RubyOnRails 2008. 1. 22. 11:43
숫자 및 텍스트를 포맷하는 도우미 메소드

- number_with_delimiter(num(숫자에서 세자리마다 쉼표를 찍어줌)
  <%= number_woth_Delimiter(1562000) %>
  => 1,562,000

- number_with_precision(num, digits): 소수를 표시할 때, 소수점 이하 자리수를 제한하여 표시해줌, 두 번째 인자는 소수점 이하 자리수를 지정함. 두 번째 인자는 선택사항으로 지정하지 않으면 소수점이 세 자리까지 표시됨
  <% number_with_precision(200.0, 1 %>
  => 66.7
  <% number_With_precision(200.0/3) %>
  => 66.667

- number_to_human_size(num) : 숫자를 바이트로 계산하여, 사람이 읽기 편한 단위(Byte, KB, MB등)로 변환해줌
  <%= number_to_human_size(31270) %>
  => 30.5KB

- truncate(str,length) : 전체 문자열의 길이가 length를 넘지 않도록 str의 끝을 잘라줌
  <%= truncate("무궁화 꽃이 피었습니다", 10) %>
  => 무궁화 꽃이 ...

- highlight(str, word): str안의 텍스트에서 word로 넘겨받은 문자열이 나타날 때마다 <strong> 태그를 사용하여 강조해줌
  <%= highlight("무궁화 꽃이 피었습니다.", "꽃") %>
  => 무궁화 <strong class=highlight">꽃</strong>이 피었습니다.

- simple_format(str) : 텍스트를 HTML로 변환

이미지 태그 도우미 메소드
- image_tag(source, html_options): <img> 태그를 출력해줌, html_options 부분은 해시로 데이터를 전달받으며, :size, :class, :alt 옵션 등을 사용할 수 있다.

<%= image_tag("company_logo.gif", :size => "80x30",
                                                   :class => "logo",
                                                   :alt => "회사 로고") %>
<img src="/images/company_logo.gif" class="logo" width="80" height="30" alt="회사 로고" />

링크 도우미 메소드
- link_to(name, options, html_options) :<a>태그를 출력해줌. options 부분은 해시로 데이터를 전달받으며, :controller, :action, :id등 해당 라우팅의 URL패턴에 사용되는 모든 요소를 옵션으로 사용할 수 있다. html_options부분은 <a>태그에서 사용되는 html옵션을 해시로 전달받으며, :class, :popup, :confirm, :method등을 사용할 수 있다.

<%= link_to("프로필 보기",
                 {:controller => "users", :action => "show", :id => @user},
                 {:popup => ['Help', 'width=320, height=400'], :class => "info"}) %>

=>
<a href="/users/show/41" class="info" onclick="window.open(this.href, '프로필','width=320, height=400');
return false;">프로필 보기 </a>