Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using form_for tag with get method

I am trying to Submit a form using get method. Earlier I was trying a similar thing with form_tag and it was working but now when I changed to a form_for tag, this doesn't seem to work.

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

I get a no routes error.

like image 623
nightf0x Avatar asked Mar 16 '12 18:03

nightf0x


People also ask

What is Form_for in Ruby?

This is the name used to generate the input's name (and the params' names), example: = form_for Admin.new, as: :user do |f| #^^^^ = f.input :username # will generate an input like this: <input type='text' name='user[username]' #... /> #

What is the difference between form for and form with in Rails?

form_for and form_tag in Rails were very similar, both allowed you to create a form tag but the first one uses model's attributes to build create or update form, while the second one simply creates an HTML form tag with the passed URL as action.

What is form tag in Ruby on Rails?

The form_tag Rails helper generates a form withe the POST method by default, and it automatically renders the HTML that we were writing by hand before. Note: We can explicitly specify what HTTP verb to use for the form_tag if we want something other than POST .


2 Answers

You can pass the raw HTML attributes using :html if you need to. For Rails 3:

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

Update and in Rails 4, per @andre.orvalho's suggestion below, the method parameter can be supplied directly:

<%= form_for(@post_filter, url: filter_path, method: :get ) do |f| %>
like image 110
rjz Avatar answered Oct 29 '22 16:10

rjz


Did you try

<%= form_for(@post_filter, :url=> filter_path, :html => {:method => :get}) do |f| %>
like image 45
Blacksad Avatar answered Oct 29 '22 17:10

Blacksad