Forum: Ruby on Rails Jquery strange behavior

Posted by Barry (Guest)
on 2013-03-12 23:57
(Received via mailing list)
Hello.
In my app jquery script runs strange and wrong. So I have usuall .js.erb
template, which is called from controller by format.js
This template is just for testing now, so it only calls alert message, 
if
some links are clicked. I didn't modify default layout document.
The problem is that on first click nothing happens, than it alerts as 
many
times, as all links was clicked until this moment.
So 1st time click - zero alerts, 2 time click - 1 alert, 3 time click - 
2
alerts and so on.
The code of Jquery file is so simple, that definetely not the source of
problem.
May be some ideas?
Posted by Barry (Guest)
on 2013-03-13 00:00
(Received via mailing list)
But for the sake of stupid errors in simple things I'll post jquery 
code:

$('.keyword_links').ready(function(){
    var keywords = $('.keyword_links');
    function addField(){
        alert('True');
    }
    keywords.bind("click", addField);
});
Posted by Robert Walker (robert4723)
on 2013-03-13 00:50
Barry wrote in post #1101300:
> But for the sake of stupid errors in simple things I'll post jquery
> code:
>
> $('.keyword_links').ready(function(){
>     var keywords = $('.keyword_links');
>     function addField(){
>         alert('True');
>     }
>     keywords.bind("click", addField);
> });

What you show here is not the proper way to use the ready AFAIK.

Try this instead:

$(document).ready(function() {
  $('.keyword_links').bind('click', addField);
});

function addField() {
  alert('True');
}

// Or the shorthand version
$(function() {
  $('.keyword_links').click(function() { alert('True') });
});

// Even more awesome use CoffeeScript
$ -> $('.keyword_links').click -> alert('True')
Posted by Robert Walker (robert4723)
on 2013-03-13 00:54
Robert Walker wrote in post #1101305:
> Barry wrote in post #1101300:
>> But for the sake of stupid errors in simple things I'll post jquery
>> code:
>>
>> $('.keyword_links').ready(function(){
>>     var keywords = $('.keyword_links');
>>     function addField(){
>>         alert('True');
>>     }
>>     keywords.bind("click", addField);
>> });
>
> What you show here is not the proper way to use the ready AFAIK.

Forgot to include the documentation reference:

http://api.jquery.com/ready/
Posted by Barry (Guest)
on 2013-03-13 01:15
(Received via mailing list)
no, this didn't solve problem


, 13  2013 ., 2:56:58 UTC+4  Barry :
Posted by Barry (Guest)
on 2013-03-13 01:15
(Received via mailing list)
no, this didn't solve this ((
Posted by Robert Walker (robert4723)
on 2013-03-13 02:19
Barry wrote in post #1101311:
> no, this didn't solve this ((

It worked as expected in my quick test. You haven't provided enough 
information for me to help further. For instance I don't know what your 
HTML looks like. How many elements are matched by the jQuery selector, 
etc.
Posted by Barry (Guest)
on 2013-03-13 03:04
(Received via mailing list)
ok...
more code.
in tests_controller method which calls js file:

def add_key
   @test=Test.find(params[:id])
   @key=@test.keys.build
   @key.keyword = params[:keyword]
   @key.position = params[:position]
   @answer = generate_answer_field(@key.position)
   respond_to do |format|
   if @key.save then
          format.js
   end
   end
   end

it calls add_key.js.erb with code from first post

in show view link on which jquery called:

<%= link_to(result_array[k], {:controller =>'tests',  :action => 
'add_key',
:keyword => result_array[k], :position=>k}, :class=>'keyword_links',
:method => :post, :remote => true) + str %>

<a href="/tests/143/add_key?keyword=Forever&amp;position=18"
class="keyword_links" data-method="post" data-remote="true"
rel="nofollow">Forever</a>

And finally to mention, that actually I don't have real route for this 
link
coz all it need to render some form and save smth to database.

Link works - saves that info to database. Problem goes with Jquery.
Posted by Barry (Guest)
on 2013-03-13 03:06
(Received via mailing list)
A lot of such links from array output. But problem is maintaining with 
one
link etc.

, 13  2013 ., 5:19:34 UTC+4  Ruby-Forum.com User
:
Posted by Barry (Guest)
on 2013-03-13 05:23
(Received via mailing list)
guys, I found out everything. As always, I'm stupid. Hope, this topic 
will
help someone to avoid this type of mistake.

So, I got controller, which respond to js.ERB file.

This js.erb file is called when controller method called, that means it
doesn't suit to handle some events inside of it.
Posted by Barry (Guest)
on 2013-03-13 05:51
(Received via mailing list)
$('#<%=j @key.position.to_s %>').hide().after('<%= j
render('generate_answer_field') %>');

works perfectly) i'm dumb)
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.