form_tag (ActionView::Helpers::FormTagHelper)

本文详细介绍了如何在Ruby on Rails应用中使用form_tag方法创建表单,包括设置表单动作、表单方法、上传文件、远程提交等功能。通过示例代码演示了各种配置选项的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


form_tag(url_for_options = {}, options = {}, *parameters_for_url, &block)

Starts a form tag that points the action to an url configured with url_for_options just like ActionController::Base#url_for. The method for the form defaults to POST.

Options
  • :multipart - If set to true, the enctype is set to “multipart/form-data”.

  • :method - The method to use when submitting the form, usually either “get” or “post”. If “put”, “delete”, or another verb is used, a hidden input with name _method is added to simulate the verb over post.

  • A list of parameters to feed to the URL the form will be posted to.

  • :remote - If set to true, will allow the Unobtrusive JavaScript drivers to control the submit behaviour. By default this behaviour is an ajax submit.

Examples
 form_tag('/posts')
 # => <form action="/posts" method="post">

 form_tag('/posts/1', :method => :put)
 # => <form action="/posts/1" method="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="submit" value="Save" /></div></form>

<%= form_tag('/posts', :remote => true) %>
 # => <form action="/posts" method="post" data-remote="true">
  # File actionpack/lib/action_view/helpers/form_tag_helper.rb, line 49
def form_tag(url_for_options = {}, options = {}, *parameters_for_url, &block)
  html_options = html_options_for_form(url_for_options, options, *parameters_for_url)
  if block_given?
    form_tag_in_block(html_options, &block)
  else
    form_tag_html(html_options)
  end
end

    <%= form_tag("#{Settings.cache_clear_link.host}&req_time=#{@time}&req_from=cms&sign=#{@sign}") do %>
      <%= submit_tag "[立即生效]" ,:class => "button" %>
    <% end %>





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值