form_tag(url_for_options = {}, options = {}, &block) public
启动一个表单标记,将操作指向一个配置了url_for_选项的url,就像ActionController::Base#url_for一样。表单的方法默认为POST。
使用方法是地址加上方法,使用get和post都可以
<%= form_tag("/search", method: "get") do %> <%= label_tag(:q, "Search for:") %> <%= text_field_tag(:q) %> <%= submit_tag("Search") %> <% end %>
<!-- <form action="/domains" method="get"> domain_name: <input type="text" name="domain_name" value=<%= params[:domain_name] %>/> domain_address: <input type="text" name="domain_address"/> <input type='submit' /> </form> --> <%= form_tag '', method: :get do %> <%= text_field_tag :domain_name, params[:domain_name] %> <% end %>
这里的地址设置的就是本页面的地址,不会跳转其他页面,' ' 和 "/domains" 的作用是一样的。都是指向原有的页面。
form_tag('/posts')
# => <form action="/posts" method="post">
form_tag('/posts/1', method: :put)
# => <form action="/posts/1" method="post"> ... <input name="_method" type="hidden" value="put" /> ...
form_tag('/upload', multipart: true)
# => <form action="/upload" method="post" enctype="multipart/form-data">
<%= form_tag('/posts') do -%>
<div><%= submit_tag 'Save' %></div>
<% end -%>
# => <form action="/posts" method="post"><div><input type="submit" name="commit" value="Save" /></div></form>
<%= form_tag('/posts', remote: true) %>
# => <form action="/posts" method="post" data-remote="true">
form_tag('http://far.away.com/form', authenticity_token: false)
# form without authenticity token
form_tag('http://far.away.com/form', authenticity_token: "cf50faa3fe97702ca1ae")
# form with custom authenticity token