解析帖子@username [英] parse a post for @username

查看:165
本文介绍了解析帖子@username的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建立了一个类似于Twitter的@replies,允许用户通过用户的日常帖子彼此联系...类似于stackoverflow.

I have established a Twitter-like @replies that allows users to contact one another through user dailyposts...similar to stackoverflow's.

以此为指南 https://github.com/kltcalamay/sample_app/compare/original-version...master

我如何解析/扫描帖子&然后将@usernames替换为该用户页面的链接

帖子示例.

Kevins Post:
 @Pzpcreations @john @steve hey everyone lets all hang out today

我要扫描/解析帖子,然后将用户@Pzpcreations @john @steve链接到他们的个人资料

I want scan/parse the post and then link the users @Pzpcreations @john @steve to their profile

我尝试在Dailypost模型中创建一个方法,该方法将用户名存储在一个数组中....但是IDK如何替换用户名并将其链接到适当的用户页面

def username_link
  str = self.content_html
  recipient = str.scan(USERNAME_REGEX)
end

这给了我["@Pzpcreations","@ john","@ steve"]

This gives me ["@Pzpcreations", "@john", "@steve"]

请帮帮我...................

Please help me out....new to rails :)

模型

class Recipient < ActiveRecord::Base
  attr_accessible :dailypost_id, :user_id

  belongs_to :user
  belongs_to :dailypost

end

class User < ActiveRecord::Base
  attr_accessible :name, :email, username

  has_many :dailyposts, dependent: :destroy

  has_many :replies, :class_name => 'Recipient', :dependent => :destroy
  has_many :received_replies, :through => :replies, :source => 'dailypost'

end

class Dailypost < ActiveRecord::Base  
  attr_accessible :content, :recipients
  belongs_to :user

  ###What is the Correct REGEX for Rails 4?
  USERNAME_REGEX = /@\w+/i

  has_many :recipients, dependent: :destroy
  has_many :replied_users, :through => :recipients, :source => "user"

  after_save :save_recipients

  **private**

    def save_recipients
      return unless reply?

      people_replied.each do |user|
        Recipient.create!(:dailypost_id => self.id, :user_id => user.id)
      end
    end

    def reply?
      self.content.match( USERNAME_REGEX )
    end

    def people_replied
      users = []
      self.content.clone.gsub!( USERNAME_REGEX ).each do |username|
        user = User.find_by_username(username[1..-1])
        users << user if user
      end
      users.uniq
    end
end

SCHEMA

create_table "recipients", :force => true do |t|
  t.string   "user_id"
  t.string   "dailypost_id"
  t.datetime "created_at",   :null => false
  t.datetime "updated_at",   :null => false
end

[#<Recipient id: 7, **user_id: "103"**, dailypost_id: "316", created_at: "2013-06-18 
10:31:16", updated_at: "2013-06-18 10:31:16">]

User_ID in recipients are the users that are mentioned in the Dailypost.

观看次数

<%= link_to dailypost.username_link %>

推荐答案

您可以将每个匹配项传递给一个块. 在此块中,您返回所需的链接,例如

You can pass each match through to a block. In this block you return the link required, something like

def username_link
  str = self.content_html
  str.gsub!(USERNAME_REGEX).each do |recipient|
    if User.find_by_name(recipient) 
      "[link to #{recipient}]"
     else 
       recipient
     end
  end
end

修改

app/helpers/posts_helper.rb

def post_with_links(post)
  post.content_html.gsub(/@\w+/).each do |username|
    user = User.find_by_username(username[1..-1])
    if user
      link_to username, user
    else
      username
    end
end

在您的视图中使用

<%= post_with_links(post) %>

这篇关于解析帖子@username的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆