Desarrollando una web con Ruby on Rails me he encontrado con un problema relacionado con los permalink y la codificación de caracteres UTF-8.
He aquí lo bueno de cada uno de estos tres elementos:
pelicula/la_guerra_de_las_galaxias" pelicula?id=3230" la-caera-est-rota" sería el permalink que no respeta acentos.la_caneria_esta_rota" es el permalink que quiero obtener (Me gusta más el guión bajo que el guión normal para indicar los espacios en blanco).He aquí las soluciones posibles que he encontrado para crear un permalink automáticamente a partir del título del artículo:
Se trata de agregar el método before_save en el modelo de la tabla artículos. Dicho método guardaría el campo título en el campo permalink. He aquí el código:
class Article < ActiveRecord::Base
def before_save
result = self.title.downcase
result.gsub!(/[áàäâå]/, 'a')
result.gsub!(/[éèëê]/, 'e')
result.gsub!(/[íìïî]/, 'i')
result.gsub!(/[óòöô]/, 'o')
result.gsub!(/[úùüû]/, 'u')
result.gsub!(/[ýÿ]/, 'y')
result.gsub!(/[ñ]/, 'n')
result.gsub!(/[ç]/, 'c')
result.gsub!(/['"]/, '')
result.gsub!(/[^a-zA-Z0-9-]/, ' '); #result.gsub!(/W/, ' ')
result.gsub!(/ +/, '_')
result.gsub!(/(_)$/, '')
result.gsub!(/^(_)/, '')
self.permalink = result
end
end
Pero Ruby no me compila archivos .rb codificados con UTF-8. En cambio en las plantillas .rhtml la codificación UTF-8 proporciona grandes ventajas ya que ahora me deja escribir directamente los acentos que antes me salían como un rombo negro con un interrogante.
Para guardar un archivo de texto con codificación de caracteres UTF-8 utilizo uno de estos programas:
He aquí el código:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Ejercicio permalink</title>
<script type="text/javascript">
function permalink(origen, destino)
{
var text = document.getElementById(origen).value.toLowerCase(); // without 'prototype' library
//var text = $(origen).value.toLowerCase(); // with 'prototype' library
text = text.replace(/[áàäâå]/g, 'a');
text = text.replace(/[éèëê]/g, 'e');
text = text.replace(/[íìïî]/g, 'i');
text = text.replace(/[óòöô]/g, 'o');
text = text.replace(/[úùüû]/g, 'u');
text = text.replace(/[ýÿ]/g, 'y');
text = text.replace(/[ñ]/g, 'n');
text = text.replace(/[ç]/g, 'c');
text = text.replace(/['"]/g, '');
text = text.replace(/[^a-zA-Z0-9-]/g, ' '); //text = text.replace(/\W/g, ' ');
text = text.replace(/\s+/g, '_');
text = text.replace(/(_)$/g, '');
text = text.replace(/^(_)/g, '');
document.getElementById(destino).value = text; // without 'prototype' library
//$(destino).value = text; // with 'prototype' library
return false;
}
</script>
</head>
<body>
<h1>Ejercicio <strong>permalink</strong></h1>
<form>
<p>Valor: <input type="text" id="article_title" value=" ¿Pero 23% qué__ utopía éste es esto? " size="80" /></p>
<p><input type="button" onclick="return permalink('article_title','article_permalink');" value="Crear permalink" /></p>
<p>Permalink: <input type="text" id="article_permalink" value="" size="80" readonly="readonly" /></p>
</form>
</body>
</html>
Y aquí está el código en funcionamiento:
Un comentario:
thankyou x 1000000000!!