当前位置: 动力学知识库 > 问答 > 编程问答 >

Search and convert a substring into a link. Rails

问题描述:

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.

网友答案:

Good candidate for a helper method:

def link_ids(string)
  string.gsub(/#(\d+)/) do
    link_to "##{$1}", mymodel_path($1)
  end
end
网友答案:
"posts #4,#5 and #6".gsub(/(\#\d+)/) { |s| 
    link_to s, :controller => :word, :action => :show, :id => s[1,10].to_i
}
分享给朋友:
您可能感兴趣的文章:
随机阅读: