使用带有get方法的form_for标记

时间:2012-03-16 18:10:04

标签: ruby-on-rails ruby routes

我正在尝试使用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| %>

我收到了无路由错误。

2 个答案:

答案 0 :(得分:23)

如果需要,您可以使用: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| %>

答案 1 :(得分:2)

你试过吗

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