With that code I can hide rows in my table by just checking the boxes
and show them by unchecking the boxes!
Now I want to do that in pure Rails 3 code with a check_box_tag, but I
don’t know where to put my jquery code. I already watched the Railcast
episode about jquery, but that didn’t helped me at all!
I hope some can help me! The best would be to get a short example how
to do that.
Have a look at the documentation for check_box_tag
It specifies that any other keys passed to options will be used as HTML
attributes. So you can pass your onclick attribute to check_box_tag in
the
options hash.
Now I want to do that in pure Rails 3 code with a check_box_tag, but I
don’t know where to put my jquery code. I already watched the Railcast
episode about jquery, but that didn’t helped me at all!
uff , your code is messy. ok here it goes. since the form is only to
render
the check_boxes you can leave it as it is but add an id to it
And my table where I want to show and hide rows like this:
What I am doing wrong???
Check to be absolutely certain you are using jQuery rather than
Prototype. While most of this code will work fine with either, the basic
accessor (shortcut for findElementById) in Prototype is $(‘theId’),
while jQuery prefers $(’#theId’). Also, attr(key) in jQuery is
readAttribute(key) in Prototype.
On Mon, Oct 24, 2011 at 10:56 AM, Walter Lee D. [email protected]wrote:
case 'show1':
}
})});
oh , ok im back.
Well first lest see what is wrong. if you have chrome or firefox right
-clk
on the page and lick inspect element, that should bring the inspector
up.
then enable the console if is not enabled.
type
$(“.show1”)
the console show show all the elements that match the selector if it
does
not you are not importing the js library. if it does then type
$(“.show1”) .hide()
see if the row disappear. if they they do then will have to deal with
the
event attachment seems it does not seem to be working.
go to the application.js and type
$(document).ready( <== this is importan so that jquery
first waits for the rows to actually exists
function(){ <== inside this function goes
the
code
$("#filters input:checkbox").click(function(){
console.log("hey there ");
});
}
);
this should make a hey there appear in the browser console everytime you
click a checkbox, dont forget to reload the page everytime you change
the
application.js file.
Im almost sure that you are missing the $(document).ready(); event , is
very
important because if the js loads before the checkboxes are rendered no
event will be attached to them. $(document).ready(); causes the
javascript
to wait untill the entire page has loaded before doing anything.
I was just only adding JQuery in my view with <%=
javascript_include_tag “http://code.jquery.com/jquery-latest.js” %>,
but not in my application.html.erb. That means that the code in the
application.js had propably no JQuery access!
My problem now is that my other normal Rails Java Helpers (see
below )are not working anymore as intended!
<%= button_to ‘Destroy’, product, :confirm => ‘Are you sure?’, :method
=> :delete %>
That one is wotking, but there is no confirm pop up!!!
I was just only adding JQuery in my view with <%=
javascript_include_tag “http://code.jquery.com/jquery-latest.js” %>,
but not in my application.html.erb. That means that the code in the
application.js had propably no JQuery access!
this like here is what tells rails what to output in the javascript_include_tag :defaults line
now jquery is set up.
My problem now is that my other normal Rails Java Helpers (see
below )are not working anymore as intended!
<%= button_to ‘Destroy’, product, :confirm => ‘Are you sure?’, :method
=> :delete %>
That one is wotking, but there is no confirm pop up!!!
Is possible that you are have installed prototype which has similar
syntax
to that of jquery.
I notice you said ‘Java’ helpers, pay attention, javascript is not
java,
they have similar syntax but that’s it.
Now what version of rails are you using, this helper changed their out
put
in rails 3.x+ from that found in rails 2.x-.
I read something about JRails. Is that an option or is there another
way???
JRuby is not a javascript+ruby package is java with a ruby sintax, is a
replacement for the ruby interpreter that lets Ruby run on the java
virtual
machine. Some gems are not compatible with jruby.
Remember, java and javascript are unrelated, the name javascript was
give to
javascript because java was popular back when javascript was been
develop.
Java is a general purpose programing language, javascript is a
scripting
language aimed mainly at the web.
read this also:
The deal with javascript and ‘the other way’ is that , since javascript
was
develop when the web was very young it has lots of short coming, so
people
start creating functions to deal with those short coming and eventually
put
them together to made a library, for example, to capture a the form with
an
id for filter without using any javascript library you would have to
type
this
document.getElementById(‘filters’)
as you can see that is a bit long, also if it were a class you would
have to
do this
document.getElementByClassName(‘show1’)
and if you have to capture a tag type
document.getElementByTagName(‘input’)
so many libraries have a function that replaces this with $( ) where $
is
the name of the function. In jquery you distinguish what is that you
want to
capture by addin a #, a . or nothing.
the result is :
document.getElementById(‘filters’)
is
$(“#filters”)
document.getElementByClassName(‘show1’)
is
$(“.show1”)
and
document.getElementByTagName(‘input’)
is
$(‘input’)
Javascript right now is the only client side programming language (that
i
know of, also google is making a new more modern one called
Darthttp://en.wikipedia.org/wiki/Dart_(programming_language) that
is suppose to fix all the short coming of javascript and you can try it
here http://try.dartlang.org/) but there are many libraries (the other
way
around you asked about maybe?)