Ruby Form

<% form_tag url_for(:action=>'test_action') do %>
also can write like this :
<% form_tag ('/index/test_action') do %>
***********************************************************************
<% form_remote_tag :url=>{:action=>"remote",:user_id=>12} do %>
if no params user_id , also can write like this:
<% form_remote_tag :url=>"remote" do %>
***********************************************************************
     <div><%= text_field_tag "text_tag", "default_content", :size => 50 %></div>  
    <div><%= text_area_tag "text_area", "default_content", :size => "80x5" %></div><br/>
   
    <div><%= password_field_tag "password" %></div><br/>
   
    <div><%= file_field_tag "file" %></div><br/>
   
    <div><%= check_box_tag "check_box1","selected" %></div>
    <div><%= check_box_tag "check_box2","selected",true %></div>
    <div><%= check_box_tag "check_box3","selected" %></div><br/>
   
    <div><%=radio_button_tag "radio_button","1 was selected"%></div>
    <div><%=radio_button_tag "radio_button","2 was selected",true%></div>
    <div><%=radio_button_tag "radio_button","3 was selected"%></div><br/>
   
    <% options = "" %>
    <% ["option1","option2","option3"].each do |option| %>
    <% options += "<option>" + option + "</option>"%>
    <% end %>
    <div><%=select_tag "select",options %></div>
      
    <%=hidden_field_tag("button_identifier")%>
   
    <div><%= image_submit_tag("/images/close.png",{:name=>"close"}) %></div><br/>
    <div><%= image_submit_tag("/images/rails.png",{:name=>"rails"}) %></div><br/>
     <%= submit_tag("Delete") %>
    <%= submit_tag("Cancel") %>
<% end %>
##########
When click image_submit_tag "rails", the paramsi are:
{"button_identifier"=>"", "rails.x"=>"36", "check_box2"=>"selected", "rails.y"=>"35", "radio_button"=>"2 was selected", "action"=>"test_action", "controller"=>"index", "text_tag"=>"default_content", "text_area"=>"default_content", "password"=>"secret", "file"=>"test.txt", "select"=>"option2"}
Controller:
   if params[:rails.x] && params[:rails.y]
render :text => "Clicked on A"
else
render :text => "Clicked on B"
end
When click submit_tag "Cancel", the paramsi are:
{"button_identifier"=>"", "check_box2"=>"selected", "commit"=>"Cancel", "radio_button"=>"2 was selected", "action"=>"test_action", "controller"=>"index", "text_tag"=>"default_content", "text_area"=>"default_content", "password"=>"", "file"=>"", "select"=>"option2"}
    if params[:commit]  == "Cancel"
render :text => "Clicked on Cancel"
else
render :text => "Clicked on Delete"
end
****************
In form_tag, if you put multiple submit button, like
<%= submit_tag("Update", :name=>"submit"%>
<%= submit_tag("Delete", :name=>"submit"%>
The value of params[:submit] depends on which button you click, either
"Update" or "Delete". However, in form_remote_tag, no matter you click
which button, params[:submit] is always "Update".
Solution to this defect:
<%= hidden_field_tag "button_identifier" %>
<%= submit_tag("Update", :name=>"submit",:onclick=>"$
('button_identifier').value='Update'") %>
<%= submit_tag("Delete", :name=>"submit", :onclick=>"$
('button_identifier').value='Delete') %>
 
给form_tag 指定 class的CSS样式
<% form_tag url_for(:action => "aaa"),:class => "clearfix setsmt" do%>
posted @ 2012-08-27 10:02  残星  阅读(711)  评论(0编辑  收藏  举报