# Using image as input type in submit\_to\_remote

**URL:** <https://www.ruby-forum.com/t/using-image-as-input-type-in-submit-to-remote/168642>\
**Category:** Rails\
**Created:** [May 23, 2009, 12:05pm UTC](https://www.ruby-forum.com/t/using-image-as-input-type-in-submit-to-remote/168642 "2009-05-23T12:05:00Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![gautam\_chekuri](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/7ab992/32.png) [@gautam\_chekuri](https://www.ruby-forum.com/u/gautam_chekuri)\
**Post date:** [May 23, 2009, 12:05pm UTC](https://www.ruby-forum.com/t/using-image-as-input-type-in-submit-to-remote/168642/1 "2009-05-23T12:05:00Z")

</div>

Hi all,

After searching for a way to set the input type of a submit tag to  
image when using submit\_to\_remote I realized that the method call  
given below, while seemingly valid, wasn’t working since the method  
button\_to\_function in actionpack/lib/action\_view/helpers/  
javascript\_helper.rb was forcing the type to button.

1. The method call in the view:  
\<%=  
submit\_to\_remote(  
‘update\_resource\_img’,  
‘Update Resource 2’,  
:url =\> { :controller =\> ‘hello’, :action =\> ‘update’ },  
:before =\> “$(‘loading’).style.display = ‘block’”,  
:after =\> “return false”,  
:html =\> { :src =\> ‘/images/button.gif’, :type =\> ‘image’ }  
)  
%\>

2. def button\_to\_function in action\_view/helpers/javascript\_helper.rb  
does the following(note the :type =\> ‘button’) :  
tag(:input, html\_options.merge(:type =\> ‘button’, :value =\>  
name, :onclick =\> onclick))

3. It is my opinion (finally :p) that the def button\_to\_remote should  
allow the input type to be either a button or an image since the w3c  
page on form input control types states that the input type image  
“Creates a graphical submit button.”  
[http://www.w3.org/TR/html401/interact/forms.html#h-17.4.1](http://www.w3.org/TR/html401/interact/forms.html#h-17.4.1)

4. Hence, to ensure that button\_to\_remote allows me to set the type of  
either of button or image, I place the following in a file in  
$RAILS\_ROOT/lib and require it at the end of $RAILS\_ROOT/config/  
environment.rb

module ActionView  
module Helpers  
module JavaScriptHelper

```
  def button_to_function(name, *args, &block)
    html_options = args.extract_options!.symbolize_keys

    function = block_given? ? update_page(&block) : args[0] || ''
    onclick = "#{"#{html_options[:onclick]}; " if html_options

```

[:onclick]}#{function};"

```
    html_options[:type] = 'button' if( !html_options[:type] ||

```

html\_options[:type] !~ /^image|button$/i )

```
    tag(:input, html_options.merge(:value => name, :onclick =>

```

onclick))  
end

```
end

```

end  
end

1. Hope you guys have an opinion to share.

- Gautam  
Programmer @ Azri

---

<div class="post-metadata">

**Author:** ![gautam\_chekuri](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/7ab992/32.png) [@gautam\_chekuri](https://www.ruby-forum.com/u/gautam_chekuri)\
**Post date:** [June 5, 2009, 8:20pm UTC](https://www.ruby-forum.com/t/using-image-as-input-type-in-submit-to-remote/168642/2 "2009-06-05T20:20:51Z")

</div>

Hi David,

yep, you are right, link\_to\_remote(image\_tag(“image”)…) would work.

In order to use link\_to\_remote I would have to either specify ID of  
the DOM element which includes all the form elements, that I want to  
submit, via the :submit argument or pass the Form.serialize Javascript  
expression via the :with argument.

While this would work, based on my understanding of things I have two  
problems with this approach:

1. Having a real Submit button would make graceful degradation easy.  
(If the onclick handler didn’t execute, the form would just do the  
traditional submit with the button. With the hyperlink I would have to  
set an alternate URL).

2. A more conceptual problem, is that it just doesn’t seem right to me  
that while link\_to\_remote works hard to soup up a hyperlink and give  
it the ability to submit a form, submit\_to\_remote, it seems, has  
decided that it will not allow an image its rightful place as a submit  
button. I would expect link\_to\_remote and submit\_to\_remote to be, kind  
of… “homomorphic”.
