Hi guys, my problem is the following, Right now I have a list of japanese words that are loaded from the database, they display okay, the problem begins when I need to split that word into ''characters'' in this case ''kanjis'' so I can make a link for each kanji,for example: **************************** I have on my view: <%= word.kanji %> which display 何曜日 But what I need is something like: <a href=''/kanjis/12''>何</a><a href=''/kanjis/55''>曜</a><a href=''/kanjis/39''>日</a> **************************** So I decided to go one step at a time, first split the thing... but using each_char didn''t work because it seems that the string ''何曜日'' is being manipulated as ''\xE4\xBD\x95\xE6\x9B\x9C\xE6\x97\xA5'' which is ASCII-8BIT. Then I tried going 3 bytes a time which allowed me to split it by kanji, it was something like this **************************** parts = [] (word.size/3).times{ |i| parts << word[i*3,3] } **************************** Now "parts" has each kanji stored and when I try to make a query with it, something like: k = Kanji.find(:first, :conditions => "kanji = ''#{parts[0]}''") everything seems ok, I even get: k.class => Kanji k.methods => (lots including :id) k.nil? => false but when I try: k.id I get: undefined method `id'' for nil:NilClass So I''m really confused, I''m pretty noob at both ruby and rails so Idk if I''m doing something really stupid or the wrong way, I''ve tried doing encode, toutf8 and stuff like that but it still gets me nowhere, I''m worried because with this I may not be able to do a search feature by kanji. Thanks in advance. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
okay lol forget about the "nil" problem I mentioned at the end, that was because out of the huge list of words there were a few that started with a symbol other than a kanji so just doing: "k.id unless k.nil?" did the trick so I''m going to change my question to... is there another(simpler) way to do this? right now my helper looks like crap, I mean... like this: ************************************* def word_to_links(word) parts, o = [], "" (word.size/3).times{ |i| parts << word[i*3,3] } parts.each do |part| k = Kanji.find(:first, :conditions => "kanjis.kanji = ''#{part}''") o << (k.nil? ? part : link_to(part, "/kanjis/#{k.id}")) end o end ************************************* Thanks. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Jun 3, 4:37 am, Japmsn Japms <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> okay lol forget about the "nil" problem I mentioned at the end, that was > because out of the huge list of words there were a few that started with > a symbol other than a kanji so just doing: > > "k.id unless k.nil?" did the trick > > so I''m going to change my question to... is there another(simpler) way > to do this? right now my helper looks like crap, I mean... like this: > > ************************************* > def word_to_links(word) > parts, o = [], "" > (word.size/3).times{ |i| parts << word[i*3,3] }I wouldn''t rely on some or most kanjis being exactly 3 bytes in utf8 - use the multibyte chars stuff in activesupport Fred> parts.each do |part| > k = Kanji.find(:first, :conditions => "kanjis.kanji = ''#{part}''") > o << (k.nil? ? part : link_to(part, "/kanjis/#{k.id}")) > end > o > end > ************************************* > > Thanks. > -- > Posted viahttp://www.ruby-forum.com/.-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Frederick Cheung wrote:> On Jun 3, 4:37�am, Japmsn Japms <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: >> � def word_to_links(word) >> � � parts, o = [], "" >> � � (word.size/3).times{ |i| parts << word[i*3,3] } > > I wouldn''t rely on some or most kanjis being exactly 3 bytes in utf8 - > use the multibyte chars stuff in activesupport > > FredThanks I''ll look into that -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.