2012-03-16 37 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| %>