2013-06-20 41 views
2

我已經建立了一個類似Twitter的@回覆,允許用戶互相接觸,通過用戶dailyposts ......類似計算器的一個職位。解析爲@username

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

如何可以解析/掃描後&然後與鏈接到該用戶的頁面

後的實例代替@使用者名稱。

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

我想掃描/解析後,然後將用戶@Pzpcreations @約翰@steve鏈接到他們的個人資料

我試圖建立在我的Dailypost型號,存儲用戶名在方法陣列....但IDK如何更換,並將其鏈接到相應的用戶頁面

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

這給了我[ 「@Pzpcreations」, 「@約翰」, 「@steve」]

請幫我....新軌道:)

模型

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. 

VIEWS

<%= link_to dailypost.username_link %> 
+1

創建一個輔助函數很好的問題,所有的重新levant data – Senjai

+0

這樣做是否正確,你真的應該使用解析器。 –

回答

2

可以通過給塊通過每個匹配。 在該塊返回所需的鏈接,像

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) %> 
+0

謝謝! :),我認爲你有額外的語法。我一路上遇到了一些錯誤@Ian Kenney –

+0

好點 - 我的錯在沒有測試代碼! –

+0

認爲你能幫助我....我仍然有路由問題。 :/ @伊恩肯尼http://stackoverflow.com/questions/17229034/href-to-users-profile-is-redirecting-me-to-user-index/17230150?noredirect=1#17230150 –

1

你希望做一個查找和每個用戶名替換,並且還產生鏈接到用戶的個人資料。這將是這個樣子:

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 

這裏假設你已經得到了你的路線文件users路線產生的users_path方法。

還有的額外的東西來談談,因爲它對於一個Twitter類似回覆系統,發現那些對自己的整體兔子洞將是一半的樂趣! )

+0

這可能存在部分匹配的問題 - 例如, \ @jo \ @joe \ @jon讓我們掛出...... –

+0

您應該可以一次執行一個查詢來查找數據庫中的所有名稱,而不是查找每個名稱的查詢。如果找不到名字會怎麼樣? – Sirupsen

+0

我已更新我的路線和控制器...任何想法爲什麼我總是得到未定義的方法'user_path'爲# @Gavin Miller –