Ruby on rails 解析@username的帖子
我已经建立了一个类似Twitter的@repress,允许用户通过用户每日帖子相互联系……类似于stackoverflow 以此为指导 如何解析/扫描帖子,然后用指向该用户页面的链接替换@usernames 一个帖子的例子 我想扫描/解析帖子,然后将用户@Pzpcreations@john@steve链接到他们的个人资料 我尝试在Dailypost模型中创建一个方法,将用户名存储在一个数组中……但是IDK如何替换并将它们链接到相应的用户页面Ruby on rails 解析@username的帖子,ruby-on-rails,ruby,replace,reply,Ruby On Rails,Ruby,Replace,Reply,我已经建立了一个类似Twitter的@repress,允许用户通过用户每日帖子相互联系……类似于stackoverflow 以此为指导 如何解析/扫描帖子,然后用指向该用户页面的链接替换@usernames 一个帖子的例子 我想扫描/解析帖子,然后将用户@Pzpcreations@john@steve链接到他们的个人资料 我尝试在Dailypost模型中创建一个方法,将用户名存储在一个数组中……但是IDK如何替换并将它们链接到相应的用户页面 def username_link str = s
def username_link
str = self.content_html
recipient = str.scan(USERNAME_REGEX)
end
这给了我[“@Pzpcreations”、“@john”、“@steve”]
请帮帮我……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
<%= link_to dailypost.username_link %>
类收件人'Recipient',:dependent=>:destroy
已收到多个回复,:至=>:回复,:source=>'dailypost'
结束
类Dailypost:收件人,:源=>“用户”
保存后:保存收件人
**私人的**
def保存_收件人
除非答复否则返回?
人们回答。每个人都是用户|
收件人。创建!(:dailypost_id=>self.id,:user_id=>user.id)
结束
结束
答复?
self.content.match(用户名\正则表达式)
结束
def people_回答
用户=[]
self.content.clone.gsub!(用户名| REGEX)。每个do |用户名|
user=user.find\u by\u用户名(用户名[1..-1])
真正的用户不会|
t、 字符串“用户id”
t、 字符串“dailypost\u id”
t、 datetime“created_at”,:null=>false
t、 datetime“updated_at”,:null=>false
结束
[#]
收件人中的用户ID是Dailypost中提到的用户。
视图
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
<%= link_to dailypost.username_link %>
您希望对每个用户名进行查找和替换,并生成指向用户配置文件的链接。它看起来像这样:
def username_link
new_content_html = self.content_html
recipients = new_content_html.scan(USERNAME_REGEX)
recipients.each do |recipient|
user = User.find_by_username(recipient) # strip off the @ if required
new_content_html.gsub!(recipient, "<a href='#{users_path(user)}'>#{user.username}</a>")
end
new_content_html
end
def username\u链接
new\u content\u html=self.content\u html
recipients=new\u content\u html.scan(用户名\u REGEX)
收件人。每个人都有|收件人|
user=user.find_by_username(recipient)#如果需要,去掉@
新建内容html.gsub!(收件人“”)
结束
新内容
结束
这假设您在路由文件中为用户
创建了一条路由,该路由文件生成用户\u路径
方法
关于类似于推特的回复系统,还有一大堆额外的东西需要讨论,而发现这些东西将是乐趣的一半!;) 您可以将每个匹配传递到块。 在这个块中,您返回所需的链接,类似于
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\u 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) %>
好问题,如果所有相关数据都正确执行此操作,您真的应该使用解析器。这可能与部分匹配有问题-例如\@jo\@joe\@jon lets out…您可能应该一次执行一个查询以查找数据库中的所有名称,而不是按找到的每个名称进行查询。如果找不到名称会发生什么?我已经更新了路由和控制器…知道为什么我一直为#@Gavin Miller获取未定义的方法“users\u path”吗?Gavin Miller提供了有关如何从ModelsHanks访问路由帮助器的详细信息!:),我认为你有额外的语法。我在路上遇到了几个错误@Ian Kenney认为你可以帮我解决……我仍然有路由问题。:/@伊恩·肯尼