# \[rails 3\] how to write an Ajax call w the new unobtrusive js?

**URL:** <https://www.ruby-forum.com/t/rails-3-how-to-write-an-ajax-call-w-the-new-unobtrusive-js/193436>\
**Category:** Rails\
**Created:** [August 17, 2010, 6:53pm UTC](https://www.ruby-forum.com/t/rails-3-how-to-write-an-ajax-call-w-the-new-unobtrusive-js/193436 "2010-08-17T18:53:17Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![kadoudal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f0a364/32.png) [@kadoudal](https://www.ruby-forum.com/u/kadoudal)\
**Post date:** [August 17, 2010, 6:53pm UTC](https://www.ruby-forum.com/t/rails-3-how-to-write-an-ajax-call-w-the-new-unobtrusive-js/193436/1 "2010-08-17T18:53:17Z")

</div>

I have current a js function call I would like to transform into an  
Ajax request

= check\_box\_tag “selection”, “1”, false, {:id =\> user.id, :onclick =\>  
“showHideMenuItem(this);”}

I found a lot of examples on link\_to , but none on a checkbox … I  
know I have to add :remote =\> true somewhere, but that’s all I  
know …

please, any help or tip will be welcome !

thanks a lot for your links…

erwin

---

<div class="post-metadata">

**Author:** ![kadoudal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f0a364/32.png) [@kadoudal](https://www.ruby-forum.com/u/kadoudal)\
**Post date:** [August 18, 2010, 11:35am UTC](https://www.ruby-forum.com/t/rails-3-how-to-write-an-ajax-call-w-the-new-unobtrusive-js/193436/2 "2010-08-18T11:35:35Z")

</div>

The rails.js UJS file appears to handle only links/anchors or forms.

I went through a “I want to do this with UJS” about a week ago. While  
I got it to work, I am not sure what I accomplished since I was not  
doing AJAX - I just wanted to replace a non-working client-side  
Javascript validation routine.

My journey is detailed in a post: \<[http://groups.google.com/group/](http://groups.google.com/group/)  
rubyonrails-talk/browse\_thread/thread/0e4cf37dbfab4d94?hl=en#\>

I’ve had limited response on rails3 questions since probably less that  
10% of those on the list are currently trying it. I did do some  
experimenting with what I think you are trying to do. One approach is  
to wrap your checkbox in a link:

```
    <%=link_to( check_box_tag( "selection", "1", false, {:id =>

```

user.id}) , “/yoururl/1”, :remote =\> true) %\> One

In tracing the javascript it will do a GET request to /yoururl/1

if you do:

```
    <%=link_to( check_box_tag( "selection", "1", false, {:id =>

```

user.id}), “/yoururl/1”, :remote =\> true, :“data-method” =\>  
“showHideMenuItem”) %\> One

That will try a POST request to /yoururl/1

If you do "

```
    <%=link_to( check_box_tag( "selection", "1", false, {:id =>

```

user.id}), “/yoururl/1” :“data-method” =\> “showHideMenuItem”) %\> One

That will act like the delete action and build a form and submit try  
to submit it.

Somewhere in there you have to deal with the checkbox. I’ve never  
experimented with a xxx.js.erb file, so I guess you do it there.

Hope this gives you a start.

---

<div class="post-metadata">

**Author:** ![kadoudal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f0a364/32.png) [@kadoudal](https://www.ruby-forum.com/u/kadoudal)\
**Post date:** [August 18, 2010, 12:14pm UTC](https://www.ruby-forum.com/t/rails-3-how-to-write-an-ajax-call-w-the-new-unobtrusive-js/193436/3 "2010-08-18T12:14:48Z")

</div>

Well, why not try observe\_field? like this i did

\<%= check\_box :campaign, :use\_global, {}, 1,0 %\>  
\<%= observe\_field :campaign\_use\_global, :url =\> {:controller =\>  
“metakeys”,:action =\> ‘update\_metakeys\_flag’, :id =\> parent\_id, :type =\>  
“campaign”}, :with =\> “‘val=’+value”, :method =\> :post %\>

Thanks

---

<div class="post-metadata">

**Author:** ![kadoudal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f0a364/32.png) [@kadoudal](https://www.ruby-forum.com/u/kadoudal)\
**Post date:** [August 18, 2010, 1:01pm UTC](https://www.ruby-forum.com/t/rails-3-how-to-write-an-ajax-call-w-the-new-unobtrusive-js/193436/4 "2010-08-18T13:01:39Z")

</div>

the remote\_function is still alive … I finally ended using it,  
and it works  
but the Rails 3 doc is not yet very clear about all this remote  
stuff…
