button_to with an image

Hi, is that possible?

I have tried many things but each of them failed. I saw there was once a
patch available some time ago, was it included in Rails?

Do you want a button or an image? If you want an image, you

link_to image_tag(...), ...

Jason

I want both.

Instead of having an ugly button, I want to display my very own image.

Use CSS to style the button, with your image as background.

FWIW,

Styling form elements like buttons isn't cross-platform portable. It
flat out doesn't work in IE 6 or below, I'm not sure of the
capabilities of IE 7. Works fine in Firefox / Safari, so if that's all
you care about then have at it.

Jason

What I want is to be able to generate html code that looks like this
using the button_to (or other means) helper:

<input type="image" src="images/submit.jpg" value="Submit" alt="Submit">

There is no need for css. I can't just type raw html, as I need the
authenticity token to be created too as data is being POSTed to the
server.

image_submit_tag

http://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html#M001046

Jason

This means I have to use the form_tag also.

But I think it is the only way to do it though. It is really strange
that the Rails team didn't think about that option for the button_to
helper.

Or use the code from button_to to make your own image_button_to
method in application_helper.rb; it really only amounts to changing
this line:

        html_options.merge!("type" => "submit", "value" => name)

to something like

        html_options.merge!("type" => "image", "src" => "/button.png",
"alt" => "go", "value" => name)

Assuming you don't want to patch the source itself to accept the
additional parameters....

FWIW,

I had a similar goal -- to build something like this:
http://particletree.com/features/rediscovering-the-button-element/
page down to "the heading "The Result" where it shows a Login button,
a Change Password link and a Cancel link.

I came up with two helpers:

Fernando Perez wrote:

This means I have to use the form_tag also.

But I think it is the only way to do it though. It is really strange
that the Rails team didn't think about that option for the button_to
helper.

If its useful, and its not already in rails, then create a patch for it,
http://rails.lighthouseapp.com

next time someone needs the functionality, they'll say;
"isn't rails great, they even thought of this circumstance"

if noone ever adds it, it'll never get added.