Make text bold question

Hello,

I´ve posted the following to the “Ruby” forum but it should be here at
“Ruby on Rails” instead. The problem is that when I perform a search the
and get the results. I want that searchwords will be highlighted as
bold, but for some reason it doesn´t work in Rails…

It works for example:
text = “This is an example text.”
search = “example text”

here are “example text” bold

but not for:
text = “This is an example of the text.”
search = “example text”

here is only “example” bold

Here´s my code:

search_controller.rb

def live_search
$KCODE = ‘latin1’
@code = $KCODE
a1 = “%”
a2 = “%”
@searchstring_name_temp = params[:searching]
@searchstring_name = @searchstring_name_temp.rstrip.gsub(" “, “%’
AND name LIKE '%”)
@searchstring_sku_temp = params[:searching]
@searchstring_sku = @searchstring_sku_temp.rstrip.gsub(” ", “%’ AND
sku LIKE '%”)

@results = Product.find_by_sql(“SELECT * FROM products WHERE name
LIKE’”[email protected]_name+a2+"’ OR sku
LIKE’"[email protected]_sku+a2+"’ order by name asc LIMIT 0,50;")

@results = Product.find_by_sql(:all, :conditions => [ "name LIKE

?", [email protected]+a2], :limit => 50)
@number_match = @results.length
render(:layout => false)
end

def highlight(text,search_string)
keywords = search_string.squeeze.strip.split(" “).compact.uniq
matcher = Regexp.new( ‘(’ + keywords.join(”|") + ‘)’ )
highlighted = text.gsub(matcher) { |match| “#{match}” }
return highlighted
end

live_search.rb

<%= @searchstring %>
<% if @results.empty? %>
not found!
<% else %>
found <%= @number_match %> time(s)!

<% @text = “This is an example of the text.”
@search = “example text” %>
<%= highlight(@text,@search) %>

<% @results.each do |row| %>

<% end %>

sku

name

<%= highlight(row.sku.capitalize,@searchstring_name_temp) %> <%= highlight(row.name.capitalize,@searchstring_name_temp) %>
<% end %>

On 12/31/07, Mark T. [email protected] wrote:

The problem is that when I perform a search the
and get the results. I want that searchwords will be highlighted as
bold, but for some reason it doesn´t work in Rails…

<%= highlight(@text,@search) %>

You’re only providing two parameters but highlight seems to require
three:

http://api.rubyonrails.com/classes/ActionView/Helpers/TextHelper.html#M001050


Greg D.
http://destiney.com/

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs