Sunday 15 May 2011

ruby on rails - parse a post for @username -


I have established a @ release like a Twitter that allows users to contact each other through daily post Is ... similar to Stakeoverflow

Using it as a guide

How can I pars / scan a post &; Then change the copy of the @username with the user names

Example of a post.

  Kevins Post: @Pzpcreations   

I have to scan / pars me posts And then users want to link @Progress @ Johns @Stest to their profile

I tried to make a method in my DailyPost model, which stores usernames in an array .... but how to convert them to idk and link to the appropriate users page

  deaf user_man_link str = self.content_html recipient = end of SRNC (USERNAME_REGEX)   

This gives me ["@pressation", "@Johns" , "@ Steve"] gives

please help me .... New to Rail :)

Model

  class recipients & lt; ActiveRecord :: Base attr_accessible: dailypost_id ,: user_id belongs_to: user belongs_to: dailypost end class user & lt; ActiveRecord :: Base Attr_accessible: Name, Email, Username: Daily: Post, Dependent :: has has_many: Answer:, class_name = & gt; 'Recipient' ,: Dependent = & gt; : Has has_many: received_replies ,: through = & gt; : Answer, source = & gt; 'Daily Post' and Class DailyPost & lt; ActiveRecord :: Base attr_accessible: content ,: recipients belong_to: user ### What is the correct REX for Railway 4? USERNAME_REGEX = / @ \ w + / i_many: Recipient, Dependent :: has_many: answer_users ,: through = & gt; : Recipient ,: Source = & gt; "User" after_save: save_recipients ** private ** def save_recipients return until answer? People_replied.each do | User | Recipient.create! (: Dailypost_id = & gt; self.id,: user_id = & gt; user.id) end-enddef answer? Self.content.match (USERNAME_REGEX) End DR Log_Repeated User = [] self.content.clone.gsub! (USERNAME_REGEX) .each do | Username User = user.find_buy_username (user name [1 ..- 1]) user & lt; & Lt; If the user ends the user end users.   

SCHEMA

  create_table "recipient",: force => Is true T | T.string "user_id" t.string "dailypost_id" t.datetime "created_at" ,: null = & gt; Incorrect t.datetime "updated_at" ,: null = & gt; False end [# & lt; Recipient ID: 7, ** User_id: "103" **, DailyPost_ID: "316", Created_TA: "2013-06-18 10:31:16", updated_at: "2013-06-18 10:31:16 "& Gt;] The recipients are user_id users who are described in the dailypost.   

VIEWS

<%> link

You can pass each match through a block. In this block, you return the necessary links, such as something

  def username_link str = self.content_html str.gsub! (USERNAME_REGEX) .each do | Recipient | If user.find_by_name (recipient) "[link to # {recipient}]" and end of receiver ending   

edit

Supporting functions in postcodeontent_html.gsub (/ @ \ w + /) apps / helpers / post_helpers.rb

  def post_with_links (post) Each user name. User = User.find_by_username (username. [1 ..- 1]) If user link_to username, user other user name expiry   

use it in your view < Pre> & lt;% = post_with_links (post)% & gt;

No comments:

Post a Comment