Blog » Blog Entry

Rails form_tag

Updated: January 1, 2008July 7, 2006

Consider the api definition for form_tag:

form_tag( url_for_options = {},
          options = {},
          *parameters_for_url,
          &proc)

This form_tag:

<%= form_tag( :action => 'upload',
              :multipart => true ) %>

Produces this:

<form action="/home/upload?multipart=true"
method="post">

which is probably not what you want.

Try this instead:

<%= form_tag( { :action => 'upload' },
                :multipart => true ) %>

and you'll get this:

<form action="/home/upload"
enctype="multipart/form-data" method="post">

You have to establish when your first hash of parameters ends, otherwise form_tag will assume your :action and your :multipart option are a single hash, when :multipart really needs to go in the second hash.

Tags: ruby, rubyonrails, form_tag, hash, multipart

« MacOS X updatedb Migration to Lighttpd »

solves the GET problem with :action specification

By: Chris <choladay at gmail dot com>

Posted: 2 years ago

I was having issues getting my form to use a GET using the form_tag helper. It could be solved by hard coding the action like form_tag('myaction', :method => :put). This hardcoding however, would fail when deployed to Mongrel because it would not prepend the action with the application name (using the --prefix=/MyApp option).

But when I tried :action=>:myaction is would just pass :method as a parameter. Enclosing the action in its own hash fixed my issue, thanks!

<%= start_form_tag({:action => :myaction}, :method => :put) %>

Handy

By: Douglas F Shearer <dougal dot s at gmail dot com>

Posted: 2 years ago

This was handy for the multipart thing.

Just as well, since I almost left on seeing the hideous font.

NEVER EVER EVER EVER use comic-sans!

http://bancomicsans.com/

Parenthesis

By: Phill Kenoyer <phill at kenoyer dot com>

Posted: 1 year ago

Don't forget to use Parenthesis around the params. Took me 20 minutes to figure out why it wasn't working.


Cookies must be enabled to make a comment