51224bdd17878b3b19e8987e9bb336a2

Let me explain the scenario
- I have table for FAQ with answer stored in that table.
- Now on FAQ page I need to show the FAQ with Answer

For Better understanding of my question please visit the following link.
http://www.yammer.com/guide/messages
On this page you can see "profile","tag","member" word has link. I want to do the same thing.

How to signup?
Ans=> First you need to "Register" with your email....etc....

On "Register" word it will be link to that page e.g http://www.domain/account/register
Now is there DRY way of doing above thing & also if in future if I need to change the above link then I should do it very easily. And there other words like "Help" which comes more than 5 times in my FAQ's. Also with some formating (new line,bold)

id
==> 1

question field has following text entry
==>  Where shall I contact?

answer field has following text entry
==> Currently application supports alerts created from any RSS feed. 
 We have a long list of alert types we're working on. please, click CONTACT_US_LINK.
def self.link_constants
    {"CONTACT_US_LINK" =>"<a href='/home/about_us?tab=contact_us'>Here</a>",
     "CONTACT_FORM_LINK" => "<a href='/home/about_us?tab=contact_us'>Contact Form</a>",
     "JOBS_PAGE_LINK" => "<a href='http://dev.fliptop.com/home/about_us?tab=jobs'>Jobs
                                                                                  Page</a>",
     "REPORT_BUG" => "<a href='/home/about_us?tab=contact_us'>Contact Us</a>",
     "REGISTER_LINK" => "<a href='/account/register'>Register</a>",
     "ACCOUNT_LINK" => "<a href='/myaccount/account_details'>Account Page</a>",
     "NEW_ALERT" => "<a href='/newalert/new_alert'>New Alert</a>",
     "RESET_PASSWORD" => "<a href='/account/forgot_password'>Reset Password</a>",
     "CUSTOMER_SERVICE" => "<a href='/home/about_us?tab=contact_us'>Customer Service</a>"      
    }
end
<% faqans = Faqanswer.find(:first) %>  # This line will move in the respective controller		   		   

 <% for cont in  Faqanswer.link_constants.keys -%>		   
   <%= faqans.gsub(cont,Faqanswer.link_constants[cont])  %>
 <% end -%>

Refactorings

No refactoring yet !

6883e5ef03484d4fcef507d7b4f1d243

al2o3cr

November 5, 2008, November 05, 2008 22:52, permalink

No rating. Login to rate!

You didn't include any code, but judging from your description, you might want to check out
ActionView::Helpers::TextHelper. (http://api.rubyonrails.org/classes/ActionView/Helpers/TextHelper.html).
The following is adapted from that code.

# links is a hash of (term => link) pairs
# terms should be in lowercase, but will match any case correctly

def make_links_in_string(string, links)
  return string if string.blank? || links.empty?
  match = links.keys.map { |p| Regexp.escape(p) }.join('|')
  string.gsub(/(#{match})/i) do |s|
    "<a href=\"links[s.downcase]\">s</a>"
  end
end

Your refactoring





Format Copy from initial code

or Cancel