[英]Search and convert a substring into a link. Rails
我們有這個字符串:“根據帖子#4,#5和#6這個單詞...”
我想使用link_to助手將“#4”,“#5”和“#6”轉換為鏈接(數字是Model的ID)
我怎樣才能做到這一點? 這難嗎? 先感謝您。
"posts #4,#5 and #6".gsub(/(\#\d+)/) { |s|
link_to s, :controller => :word, :action => :show, :id => s[1,10].to_i
}
輔助方法的最佳候選人:
def link_ids(string)
string.gsub(/#(\d+)/) do
link_to "##{$1}", mymodel_path($1)
end
end
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.