creating link_to using regex from a comment
<p>I'm trying to create a helper that finds any thing that starts with a # that has numbers afterwards separated by spaces, then remove the #, and turn it into a link.</p>

<p>However I'm not entirely sure what I'm doing wrong, right now the comment is appearing blank.</p>

<p>How can I do this correctly? </p>

<p>So far I have:</p>

<pre><code>module ApplicationHelper
    def linkhelper (comment)
        link = comment.to_s.gsub (/a?#\d.*\d/)
        if Post.friendly.exists?(@link)
            boardid = self.board_id
            postid = link
            link_to "#link", boardid.postid, :anchor =&gt; link
        elsif Reply.exists?(:pid =&gt; @link)
            boardid = self.board_id
            postid = reply.link.post_id
            link_to '#link', boardid.postid, :anchor =&gt; link
        else '#link'
        end
    end
end
</code></pre>

<p>In my view:</p>

<pre><code>&lt;% linkhelper(post.comment) %&gt;
</code></pre>

<p><strong>Update:</strong><br>
Currently my site is set up like this: board has many posts, and each post has many replies. When a post is made it checks what the highest <code>pid</code> is between all posts and replies for that board, and sets it <code>maximum pid + 1</code>. </p>

<p>What I'm trying to do is:</p>

<p>match <code>#123</code>, remove the <code>#</code></p>

<p>If it matches any <code>Post.pid</code> get <code>post.board_id</code>, replace <code>#123</code> with a <code>link_to #123, board_id/123</code>.     </p>

<p>elsif if it matches any Reply.pid get <code>reply.post_id</code> and <code>reply.board_id</code>. replace #123 with <code>link_to #123, board_id/post_id#123</code>.</p>

<p>else just put <code>#123</code>.</p>

<p><strong>The code I ended up using:</strong></p>

<pre><code>def linkhelper(comment)
  comment.to_s.gsub (/#(\d+)+/) do |match|
    slug = $1.strip
    if (post = Post.friendly.find_by(pid: slug))
      link_to "&gt;&gt;#{slug}", "#{post.board_name}/thread/#{slug}##{slug}"
    elsif (reply = Reply.find_by(pid: slug.to_i))
      link_to "##{slug}", "#{reply.board_name}/thread/#{reply.post_pid}##{slug}"
    else
      "##{slug}"
    end
  end
end
</code></pre>