2012-03-16 70 views
8

我正在尝试使用get方法提交表单。早些时候,我尝试了一个类似的东西与form_tag,它正在工作,但现在当我改变到form_for标签,这似乎并没有工作。使用form_for标记与get方法

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

我得到一个没有路线错误。

回答

18

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

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

更新和轨道4,每低于@ andre.orvalho的建议下,method参数可以直接提供:

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

谢谢你的工作。 – nightf0x 2012-03-16 18:16:29

+1

你应该更新你对rails 4的回答:<%= form_for(@post_filter,url:filter_path,method :: get)do | f | %> – 2014-06-12 10:51:38

+0

如何将url参数添加到url路径? – 2017-12-29 08:50:50

2

你尝试

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