簡體   English   中英

Rails 3-使某個正則表達式的每個字符串都具有內容的javascript onmouseover

[英]Rails 3 - make every string of a certain regex to a javascript onmouseover with content

在我的應用程序中,我有一個文章和評論模型。 我實現了回復功能,以便用戶只需將要引用的評論的ID放在評論表單中即可回復另一個評論,例如:#26(引用ID為26的評論)。 使用regex並回復user_id attr,一切正常。
從我的comments_controller中提取創建動作:

  if @comment.content.match(/(#([1-9]+))\s/)
  iteration_fragment = $2
  iteration_id = %Q{#{ iteration_fragment }}
  if @replied_to_comment = @article.comments.where(:iteration_id => iteration_id).first
    @comment.in_reply_to_user_id = @replied_to_comment.user_id
  end
  end

現在,無需創建新模型,我只想在評論系統上放置一個“ html幫助程序層”,以便將這些片段(例如“#33”)自動轉換為“#33” onmouseover鏈接,顯示評論33 onmouseover的內容。 因此,我不想引用bb代碼,而要做到極簡。
有人知道我在找什么以及應該如何處理嗎?

“自動”一詞使我認為,唯一的方法就是使用jQuery。

$("input#your_selected_input").keyup(function(){
  $this = $(this);
  if ($this.val().match(/(#([1-9]+))\s/) {
    $matches = $this.val().match(/(#([1-9]+))\s/
    $this.parent().after($this.html().replace( $matches[0], $("<a/>").html()));
  }
});

那是自動的部分。

要使頁面加載Rails中的內容,將類似於:

在您的評論模型中

def special_sauce_text
   matches = content.match(/(#([1-9]+))\s/)
   magic_sauce_links = []
   new_content = content
   if matches.present?
     for match in matches do
       magic_sauce_link << link_to(match, match, :class => 'your-special-stuff')
       new_content.gsub(match, magic_sauce_link)
     end
   end
   return new_content
end

然后,您可以使用以下內容自由地應用它:

Comment.special_sauce_text

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM