【问题标题】:Using form_for tag with get method使用带有 get 方法的 form_for 标签
【发布时间】:2012-03-16 18:10:03
【问题描述】:

我正在尝试使用 get 方法提交表单。早些时候我用 form_tag 尝试了类似的事情,它正在工作,但现在当我更改为 form_for 标签时,这似乎不起作用。

<%- filter_path = params[:action] == "index" ? posts_path : sneak_peek_posts_path %>
<%= form_for(@post_filter, :url=> filter_path, :method => :get) do |f| %>

我收到无路由错误。

【问题讨论】:

    标签: ruby-on-rails ruby routes


    【解决方案1】:

    如果需要,您可以使用 :html 传递原始 HTML 属性。对于 Rails 3:

    <%= form_for(@post_filter, :url=> filter_path, :html => { :method => 'GET' }) do |f| %>
    

    更新,在 Rails 4 中,根据@andre.orvalho 下面的建议,可以直接提供method 参数:

    <%= form_for(@post_filter, url: filter_path, method: :get ) do |f| %>
    

    【讨论】:

    • 你应该更新你对 rails 4 的回答:
    • 仅使用 method: :get 而不使用 :html =&gt; 也适用于 Rails 3.2。
    【解决方案2】:

    你试过了吗

    <%= form_for(@post_filter, :url=> filter_path, :html => {:method => :get}) do |f| %>
    

    【讨论】:

      猜你喜欢
      • 2018-12-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多