We have this string: "according to posts #4,#5 and #6 the word..."
I want to convert the "#4", "#5" and the "#6" to links, using the link_to helper (the numbers are ids of a Model)
How can I do this? Is this difficult? Thank you in advance.
We have this string: "according to posts #4,#5 and #6 the word..."
I want to convert the "#4", "#5" and the "#6" to links, using the link_to helper (the numbers are ids of a Model)
How can I do this? Is this difficult? Thank you in advance.
"posts #4,#5 and #6".gsub(/(\#\d+)/) { |s|
link_to s, :controller => :word, :action => :show, :id => s[1,10].to_i
}
Good candidate for a helper method:
def link_ids(string)
string.gsub(/#(\d+)/) do
link_to "##{$1}", mymodel_path($1)
end
end