# Using an image button for "link\_to\_remote"

**URL:** <https://www.ruby-forum.com/t/using-an-image-button-for-link-to-remote/99314>\
**Category:** Rails\
**Created:** [July 21, 2006, 6:04pm UTC](https://www.ruby-forum.com/t/using-an-image-button-for-link-to-remote/99314 "2006-07-21T18:04:20Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [July 21, 2006, 6:04pm UTC](https://www.ruby-forum.com/t/using-an-image-button-for-link-to-remote/99314/1 "2006-07-21T18:04:20Z")

</div>

All,

I want to do an AJAX request using a custom image button. I currently  
have this as a standard button action using “button\_to”.

What I need is the button equivalent of “link\_to\_remote”. Is there a  
helper that I can use, or do I need to build it by hand?

I see button\_to\_function(), but that sets me up to call a Javascript  
function, not post to a Rails action.

Or can I just do a link\_to\_remote and force it to be a POST?

Thanks,  
Wes

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [July 21, 2006, 6:17pm UTC](https://www.ruby-forum.com/t/using-an-image-button-for-link-to-remote/99314/2 "2006-07-21T18:17:38Z")

</div>

Can I just call an RJS template which will update the AJAX target DIV  
appropriately, so that I can still use button\_to for a regular target?

Wes

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [July 22, 2006, 12:35am UTC](https://www.ruby-forum.com/t/using-an-image-button-for-link-to-remote/99314/3 "2006-07-22T00:35:26Z")

</div>

Looks like “button\_to\_function” and “remote\_function” should do it?

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [July 22, 2006, 1:14am UTC](https://www.ruby-forum.com/t/using-an-image-button-for-link-to-remote/99314/4 "2006-07-22T01:14:14Z")

</div>

Actually I ended up with two options to handle this (both should degrade  
gracefully if I understand the :html option correctly).

1. Put all of the AJAX-y ness into the submit button.  
\<%= form\_tag %\>  
\<%= submit\_to\_remote(‘upload\_list’, ‘Upload a List’,  
:url =\> {:action =\> ‘target\_list\_upload\_display’},  
:html =\> {:action =\> ‘target\_list\_upload’, :class  
=\> ‘additem’}) %\>  
\<%= end\_form\_tag %\>

2. Make the form handle the AJAX call and then use a regular submit tag.  
\<%= form\_remote\_tag (:url =\> {:action =\> ‘target\_list\_upload\_display’},  
:html =\> {:action =\> ‘target\_list\_upload’})%\>  
\<%= submit\_tag(‘Upload a List’, :class =\> ‘additem’) %\>  
\<%= end\_form\_tag %\>

I think I’m going to go with #1 just because everything is encapsulated  
in the submit.

In this case, I think the solutions are interchangeable. Can anyone  
give me a good reason to favor one over the other?

Thanks,  
Wes

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [July 22, 2006, 7:46am UTC](https://www.ruby-forum.com/t/using-an-image-button-for-link-to-remote/99314/5 "2006-07-22T07:46:20Z")

</div>

Doug D. wrote:

> Wes, thank for posting answers to your questions!
> 
> It sounds you’ve gotten things under control, but for future references,  
> link\_to\_remote(“Submit”, {:method =\> :post, :url =\> {…}}). Actually, I  
> think it does a post by default.
> 
> Doug.

Doug,

You know, it’s funny - I had read the part of the link\_to documentation  
where it said that you could do a post. Sigh. This is much more  
straightforward. I’ll give it a shot.

Thanks,  
Wes

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [July 22, 2006, 7:24am UTC](https://www.ruby-forum.com/t/using-an-image-button-for-link-to-remote/99314/6 "2006-07-22T07:24:53Z")

</div>

Wes, thank for posting answers to your questions!

It sounds you’ve gotten things under control, but for future references,  
link\_to\_remote(“Submit”, {:method =\> :post, :url =\> {…}}). Actually, I  
think it does a post by default.

Doug.

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [July 22, 2006, 8:34am UTC](https://www.ruby-forum.com/t/using-an-image-button-for-link-to-remote/99314/7 "2006-07-22T08:34:16Z")

</div>

Doug,

This works well. However, I have a silly CSS question. I styled my   
with display:block so that I could move it down a bit with margin-top.

Now, when I click it, the whole element is surrounded by a border  
(even though the style on the in my stylesheet is ‘none’.

Any ideas?

Thanks again,  
Wes

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [July 22, 2006, 12:28pm UTC](https://www.ruby-forum.com/t/using-an-image-button-for-link-to-remote/99314/8 "2006-07-22T12:28:22Z")

</div>

Hi Wes,

Did you specify a style for all of the different states? This should  
disable the border for all link states:

a:link, a:visited, a:active, a:hover {border: none;}

Tom

On 7/22/06, Wes G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Thanks again,  
> Wes
> 
> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

–  
Tom D.

[http://atomgiant.com](http://atomgiant.com)  
[http://gifthat.com](http://gifthat.com)

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [July 22, 2006, 7:55pm UTC](https://www.ruby-forum.com/t/using-an-image-button-for-link-to-remote/99314/9 "2006-07-22T19:55:48Z")

</div>

Weird, that doesn’t help.

I also notice if I switch my style so that’s it’s displayed as an  
inline element, that the border shows up just around the as well.  
What gives?

wg

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [July 22, 2006, 8:07pm UTC](https://www.ruby-forum.com/t/using-an-image-button-for-link-to-remote/99314/10 "2006-07-22T20:07:09Z")

</div>

Duh - that’s why.

This is an AJAX call, and so the page isn’t refreshing. The border  
always shows up around a button when you click it, but the page isn’t  
being refreshed to restore the original link look and feel.

Luckily, I already have an RJS page to handle this :).

Wes

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [July 22, 2006, 9:26pm UTC](https://www.ruby-forum.com/t/using-an-image-button-for-link-to-remote/99314/11 "2006-07-22T21:26:55Z")

</div>

Got it!

In order to avoid the irritating “image border after link clicked”  
effect lingering, you have to effectively refresh the anchor display,  
which you can do by refactoring just the anchor into a partial,  
enclosing it in a div, and then doing a replace\_html on the div. Like  
so:

View:

 \<%= render(:partial =\> 'upload\_link') %\>

(\_upload\_link.rhtml has the anchor tag display inside of it)

RJS:  
page[:upload\_link\_container].replace\_html :partial =\> ‘upload\_link’

Very nice.

RJS - powerful magic.

Also, much thanks to RailsWeenie for this debugging script:  
[http://rails.techno-weenie.net/tip/2005/12/20/debugging\_your\_rjs\_calls](http://rails.techno-weenie.net/tip/2005/12/20/debugging_your_rjs_calls)

It is invaluable.

Wes
