2008年5月8日 星期四

form helper

form_tag:   和model無關
ex:
<% form_tag do %>
<p><label for=”login”>Login</label>
<%= text_field_tag  ‘login’ %></p>
<p><%= submit_tag ‘Log in’ %> </p>
<% end %>

1.如果form_tag後沒有接action,那麼action就是form目前所在的
  頁面對應到的method
2存取使用者輸入的資料,例如param, such as params[:login], log對應
  到text_filed_tag的值

ex:
<% form_tag  :action=>'play'  do %>
<p><label for=”login”>Login</label>
<%= text_field_tag  'car',  ‘login’ %></p>
<p><%= submit_tag ‘Log in’ %> </p>
<% end %>
1. text_field_tag後接的car是model name,login是car的attribute
   , the controller can access it using  params[:car][:login]


submit_tag 後接的字串是submit button上的文字

form_for : 和model有關
ex:
 <% form_for :person, @person, 
    :url => { :action => "create" } do |f| %>
      <%= f.text_field :first_name %>
      <%= f.text_field :last_name %>
      <%= submit_tag 'Create' %>
  <% end %>

對應的html
 <form action="/persons/create" method="post">
      <input id="person_first_name" name="person[first_name]" size="30" type="text" />
      <input id="person_last_name" name="person[last_name]" size="30" type="text" />
    <input name="commit" type="submit" value="Create" />
    </form>

存取使用者輸入的資料:
以上面為例子,Person.new(params[:person] )

ex:
<% form_for :rabbit ,  url=>{ :controller=>:rabbit, :action=>create},
      :html=> {:id=>’peter’ } do |t| %>
對應的html
<form action="/rabbit/create" id="peter" method="post">

text_field & text_field_tag
regular form helper is usually end with tag
model form helper is usually not end with tag 
ex:
text_field :customer, :name
create customer in the action of controller:
@customer = Customer.new(params[:customer])

text_field:
hidden_field:
password_field:
text_area:
radio_button(:variable, :attribute,  tag_value,  options):
check_box:


select:
selection list
ex:



沒有留言: