# AJAX: link\_to\_remote creates a post request

**URL:** <https://www.ruby-forum.com/t/ajax-link-to-remote-creates-a-post-request/165432>\
**Category:** Rails\
**Created:** [April 13, 2009, 11:15pm UTC](https://www.ruby-forum.com/t/ajax-link-to-remote-creates-a-post-request/165432 "2009-04-13T23:15:43Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jbhruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/439d5e/32.png) [@jbhruby](https://www.ruby-forum.com/u/jbhruby)\
**Post date:** [April 13, 2009, 11:15pm UTC](https://www.ruby-forum.com/t/ajax-link-to-remote-creates-a-post-request/165432/1 "2009-04-13T23:15:43Z")

</div>

I’ve tried googling this and simply can’t believe this problem hasn’t  
come up before but I’ll explain it as I’m getting it here.

I’m using RESTful actions in my controllers and trying to introduce a  
little AJAX fun to my app. Basically plan is for the user to click a  
link which shows a div containing a form to fill in, the form is then  
submitted by AJAX and the page updated accordingly.

If I create a link using link\_to\_remote to show the div that contains  
the form, it sends a post request to the server. This is fine so long as  
there’s no javascript response to handle the request in the CREATE  
method, the request presumably just gets ignored.

However now the form is being submitted by AJAX and it sends a js  
formatted post request to the controller, which interprets the request  
and updates the page accordingly. Now when I click the link to show the  
form, that post request gets sent and handled by the create action. It  
doesn’t pass validations as there’s no data in it and just causes a  
blank page to load.

So instead of even seeing the form the user gets redirected to a blank  
page.

I know I could probably just change the link\_to\_remote action to a get  
request instead but that doesn’t seem like a solid solution. I mean what  
if I want a seperate js get request as well?

Is there any way to use link\_to\_remote to create AJAX animations and  
show/hide elements without creating any request to the server?

Thanks

---

<div class="post-metadata">

**Author:** ![jbhruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/439d5e/32.png) [@jbhruby](https://www.ruby-forum.com/u/jbhruby)\
**Post date:** [April 14, 2009, 12:10am UTC](https://www.ruby-forum.com/t/ajax-link-to-remote-creates-a-post-request/165432/2 "2009-04-14T00:10:43Z")

</div>

On Mon, Apr 13, 2009 at 2:15 PM, Jon H.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Is there any way to use link\_to\_remote to create AJAX animations and  
> show/hide elements without creating any request to the server?

AJAX /means/ creating a request to the server. If you just want to do  
“animations and show/hide elements” use plain old JavaScript.

## FWIW,

Hassan S. ------------------------ removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![jbhruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/439d5e/32.png) [@jbhruby](https://www.ruby-forum.com/u/jbhruby)\
**Post date:** [April 14, 2009, 1:13am UTC](https://www.ruby-forum.com/t/ajax-link-to-remote-creates-a-post-request/165432/3 "2009-04-14T01:13:00Z")

</div>

Thanks guys, major brain malfunction there!

---

<div class="post-metadata">

**Author:** ![jbhruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/439d5e/32.png) [@jbhruby](https://www.ruby-forum.com/u/jbhruby)\
**Post date:** [April 14, 2009, 12:13am UTC](https://www.ruby-forum.com/t/ajax-link-to-remote-creates-a-post-request/165432/4 "2009-04-14T00:13:39Z")

</div>

You are looking for link\_to\_remote that will give you the ability to  
show/hide a div.

Here is an example to just show a div

link\_to\_function(“show form”, nil) { |page|  
page.visual\_effect :toggle\_blind, ‘form\_div’ }

If you want you can have a hide link inside the div that his hidden so  
they can close the form again…
