# Ajax test not working

**URL:** <https://www.ruby-forum.com/t/ajax-test-not-working/223957>\
**Category:** Rails\
**Created:** [October 24, 2012, 8:38pm UTC](https://www.ruby-forum.com/t/ajax-test-not-working/223957 "2012-10-24T20:38:50Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![aris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/258eb7/32.png) [@aris](https://www.ruby-forum.com/u/aris)\
**Post date:** [October 24, 2012, 8:38pm UTC](https://www.ruby-forum.com/t/ajax-test-not-working/223957/1 "2012-10-24T20:38:50Z")

</div>

Am having real probs trying to use Ajax with my Rails app. I have set up  
a test as

follows:

in javascript file:

window.onload = function() {  
$(’#test’).bind(‘ajax:success’, function() {  
alert(“success”);  
};  
}  
in view:

\<%= link\_to “test”, { :action =\> :getDiagram }, :remote =\> true, :id =\>  
“test” %\>  
in controller:

def getDiagram

end

Now I know this looks odd with the empty controller action, but I would  
expect this code to just show a popup window with ‘success’ and leave  
the current page loaded when the link is clicked? Instead i get the  
missing template message like its trying to load a page synchronously  
rather than using ajax?

Can anyone get my test to work? Do I need to upgrade or add a gem file?

Thanks in advance

Jason

---

<div class="post-metadata">

**Author:** ![jasonwold](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e36b37/32.png) [@jasonwold](https://www.ruby-forum.com/u/jasonwold)\
**Post date:** [October 25, 2012, 2:43pm UTC](https://www.ruby-forum.com/t/ajax-test-not-working/223957/2 "2012-10-25T14:43:22Z")

</div>

Maybe try in controller:

def getDiagram

respond\_to do |format|  
format.js  
end

end

---

<div class="post-metadata">

**Author:** ![jasonwold](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e36b37/32.png) [@jasonwold](https://www.ruby-forum.com/u/jasonwold)\
**Post date:** [October 25, 2012, 2:46pm UTC](https://www.ruby-forum.com/t/ajax-test-not-working/223957/3 "2012-10-25T14:46:18Z")

</div>

On Wednesday, October 24, 2012 7:40:24 PM UTC+1, [Ruby-Forum.com](http://Ruby-Forum.com) User  
wrote:

> ```
> alert("success");
> };
> 
> ```
> 
> }

This javascript is syntactically incorrect - you’re not closing the  
parens  
you open just after “bind”  
Also if you want to render nothing you do need to explicitly say that  
(eg  
render :nothing =\> true) or rails will complain that there is no  
template

Fred

---

<div class="post-metadata">

**Author:** ![jasonwold](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e36b37/32.png) [@jasonwold](https://www.ruby-forum.com/u/jasonwold)\
**Post date:** [October 25, 2012, 9:09pm UTC](https://www.ruby-forum.com/t/ajax-test-not-working/223957/4 "2012-10-25T21:09:30Z")

</div>

Jason W. wrote in post #1080993:

> Am having real probs trying to use Ajax with my Rails app. I have set up  
> a test as
> 
> follows:
> 
> in javascript file:
> 
> window.onload = function() {  
> $(‘#test’).bind(‘ajax:success’, function() {  
> alert(“success”);  
> };  
> }  
> in view:
> 
> \<%= link\_to “test”, { :action =\> :getDiagram }, :remote =\> true, :id =\>  
> “test” %\>  
> in controller:
> 
> def getDiagram
> 
> end
> 
> Now I know this looks odd with the empty controller action, but I would  
> expect this code to just show a popup window with ‘success’ and leave  
> the current page loaded when the link is clicked? Instead i get the  
> missing template message like its trying to load a page synchronously  
> rather than using ajax?
> 
> Can anyone get my test to work? Do I need to upgrade or add a gem file?
> 
> Thanks in advance
> 
> Jason

What I have noticed is that

1. The javascript syntax is not correct
2. You didn’t specify a controller from the link
3. It is probably make more sense to test with Json response using  
respond\_to :json  
respond\_with { :status =\> ‘okay’ }

---

<div class="post-metadata">

**Author:** ![jasonwold](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e36b37/32.png) [@jasonwold](https://www.ruby-forum.com/u/jasonwold)\
**Post date:** [October 25, 2012, 10:16pm UTC](https://www.ruby-forum.com/t/ajax-test-not-working/223957/5 "2012-10-25T22:16:56Z")

</div>

AndreM wrote in post #1081168:

> Maybe try in controller:
> 
> def getDiagram
> 
> respond\_to do |format|  
> format.js  
> end
> 
> end

Hi Andre

Tried that - dont get error but renders a blank template. My main  
concern is why the event isnt firing and showing the alert.

---

<div class="post-metadata">

**Author:** ![jasonwold](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e36b37/32.png) [@jasonwold](https://www.ruby-forum.com/u/jasonwold)\
**Post date:** [October 25, 2012, 10:20pm UTC](https://www.ruby-forum.com/t/ajax-test-not-working/223957/6 "2012-10-25T22:20:07Z")

</div>

Frederick C. wrote in post #1081170:

> On Wednesday, October 24, 2012 7:40:24 PM UTC+1, [Ruby-Forum.com](http://Ruby-Forum.com) User  
> wrote:
> 
> > ```
> > alert("success");
> > };
> > 
> > ```
> > 
> > }
> 
> This javascript is syntactically incorrect - you’re not closing the  
> parens  
> you open just after “bind”  
> Also if you want to render nothing you do need to explicitly say that  
> (eg  
> render :nothing =\> true) or rails will complain that there is no  
> template
> 
> Fred

Thanks Fred

I spotted that using Firebug, but still makes no difference. When  
setting render :nothing =\> true it just renders a blank template which  
is not what i want. The ajax:success event is not firing either

---

<div class="post-metadata">

**Author:** ![jasonwold](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e36b37/32.png) [@jasonwold](https://www.ruby-forum.com/u/jasonwold)\
**Post date:** [October 26, 2012, 4:46pm UTC](https://www.ruby-forum.com/t/ajax-test-not-working/223957/7 "2012-10-26T16:46:57Z")

</div>

On Oct 25, 2012, at 4:32 PM, Jason W. wrote:

> > > $(‘#test’).bind(‘ajax:success’, function() {
> > > 
> > > Thanks in advance  
> > > Thanks Webber  
> > > ^  
> > > The parser sees this line as a method with a block and then the =\> is  
> > > confusing (not allowed there, it isn’t in a Hash)

Either of these changes will do what you expect:

```
respond_with({ :status => 'okay' }) # no longer looks like a 

```

block  
respond\_with :status =\> ‘okay’ # must be a hash literal  
respond\_with( :status =\> ‘okay’ ) # must be a hash literal

Although the value for :status might need to be ‘OK’ or 200 rather than  
‘okay’ and you need a respond\_to in the controller.

> **[ActionController::MimeResponds#respond\_with - APIdock](https://apidock.com/rails/ActionController/MimeResponds/respond_with)**
>
> ActionController::MimeResponds#respond\_with - Ruby on Rails API documentation. View source code and usage examples.

-Rob

---

<div class="post-metadata">

**Author:** ![jasonwold](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e36b37/32.png) [@jasonwold](https://www.ruby-forum.com/u/jasonwold)\
**Post date:** [October 25, 2012, 10:32pm UTC](https://www.ruby-forum.com/t/ajax-test-not-working/223957/8 "2012-10-25T22:32:42Z")

</div>

webber han wrote in post #1081214:

> Jason W. wrote in post #1080993:
> 
> > Am having real probs trying to use Ajax with my Rails app. I have set up  
> > a test as
> > 
> > follows:
> > 
> > in javascript file:
> > 
> > window.onload = function() {  
> > $(‘#test’).bind(‘ajax:success’, function() {  
> > alert(“success”);  
> > };  
> > }  
> > in view:
> > 
> > \<%= link\_to “test”, { :action =\> :getDiagram }, :remote =\> true, :id =\>  
> > “test” %\>  
> > in controller:
> > 
> > def getDiagram
> > 
> > end
> > 
> > Now I know this looks odd with the empty controller action, but I would  
> > expect this code to just show a popup window with ‘success’ and leave  
> > the current page loaded when the link is clicked? Instead i get the  
> > missing template message like its trying to load a page synchronously  
> > rather than using ajax?
> > 
> > Can anyone get my test to work? Do I need to upgrade or add a gem file?
> > 
> > Thanks in advance
> > 
> > Jason
> 
> What I have noticed is that
> 
> 1. The javascript syntax is not correct
> 2. You didn’t specify a controller from the link
> 3. It is probably make more sense to test with Json response using  
> respond\_to :json  
> respond\_with { :status =\> ‘okay’ }

Thanks Webber

1. Noticed the missing bracket and fixed.
2. Have set a route as follows:  
match ‘/getdiagram’, :to =\> ‘prosesses#getDiagram’  
It seems to be executing the right controller/action from the error  
message i get
3. Tried that but get:  
c:/Users/Jason/rails\_projects/procstor/app/controllers/prosesses\_controller.rb:123:  
syntax error, unexpected tASSOC, expecting ‘}’  
respond\_with { :status =\> ‘okay’ }  
^

---

<div class="post-metadata">

**Author:** ![jasonwold](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e36b37/32.png) [@jasonwold](https://www.ruby-forum.com/u/jasonwold)\
**Post date:** [October 29, 2012, 3:48pm UTC](https://www.ruby-forum.com/t/ajax-test-not-working/223957/9 "2012-10-29T15:48:57Z")

</div>

dude, you miss a lot.  
in \*.js.erb  
blablabla, looks fine  
in controler  
you miss format.js  
in router  
you miss  
resource contoler\_name do  
get: :getDiagram on :member # or post or whatever, that will be  
your  
method name in link\_to tag  
end  
in erb tamplate  
\<%= link\_to “Button”, controler\_action\_path, method: get, remote: true  
=\>  
that all, you should try it now, and should works

在 2012年10月25日星期四UTC+8上午2时40分24秒，[Ruby-Forum.com](http://Ruby-Forum.com) User写道：
