Categories
Programming

Pattern Matching Function (Ruby)

def hilite_search_criteria(pattern, search_criteria)
return if pattern.nil? or search_criteria.nil?

pattern.gsub!(/(<[^>]*>)|\n|\t/s) {” “}

match_char = pattern =~ /#{search_criteria}/i
matched_string = $~.to_s

return if match_char.nil?
start = match_char – 25
start = (start < 0 ? 0 : start) unless match_char.nil? p = pattern[start ... match_char + search_criteria.length + 25] match_char = pattern =~ /#{search_criteria}/i p[match_char, matched_string.length ] = '‘ + matched_string + “
return p
end
end

Categories
Programming

How to get the current action & controller name in Rails

In your views:

controller.action_name
controller.controller_name

In the controller itself, you can just call

controller_name
action_name

If you want the current URI, try:

request.request_uri
Categories
Programming

If an element is visible or hidden using jQuery

Usually you can trust the .toggle function (or .slideToggle) to hide or show whatever you want it to. But occasionally you need to know if something is hidden or shown.

var isVisible = $(‘#giftMsg’).is(‘:visible’);
var isHidden = $(‘#giftMsg’).is(‘:hidden’);

unsolved: jQuery: Can you redefine the click function on a button? (unsolved mystery)

I have defined a click function in the document ready

$(“#submit_request_toggle”).click(function() {
$(‘#submit_request’).slideToggle(500);
});

Later, I do an ajax call and I want to be able to redefine the click so that the link doesn’t work anymore (after my ajax call)

$.ajax({
type: “POST”,
url: “index.php”,
data: “action=submit_request&first_name=” + $(‘#submit_request_form input[name=first_name]’).val() + “&last_name=” + $(‘#submit_request_form input[name=last_name]’).val() + “&message=” + $(‘#submit_request_form textarea[name=message]’).val(),
success: function(msg){

$(“#submit_request_toggle”).click(function() { return false; });
$(“#submit_request”).fadeOut(500, function() { $(“#submit_request_response”).html(msg).fadeIn(); } );
}

For some reason, this line:

$(“#submit_request_toggle”).click(function() { return false; });

doesn’t work– the link still shows the hidden div even after I redefine it. Weird.