0

i would like to replace the last occurence of block ({{{GUEST_COUPON_DATE_LIMIT_LINE}}}). I tried to write :

 @company_page.html = @company_page.html.reverse.sub(/{{{GUEST_COUPON_DATE_LIMIT_LINE}}}/, '').reverse

but i get error saying wrong number of arguments (given 0, expected 2). Probably I am writing wrong, so how can I fix this code?

hika
  • 1
  • 1
  • Hi Jad, thank you for your reply! when i try this, i get error saying "undefined method `sub' ". – hika Jun 28 '21 at 10:41
  • what do you get from `@company_page.html.class`? it's hard to know what methods there are, unless we know the class of the object. – Jad Jun 28 '21 at 10:44
  • 1
    BTW, if `html` returns the page's body as a string and you reverse that string for replacement purposes, you also have to reverse the replacement, i.e. use `}}}ENIL_TIMIL_ETAD_NOPUOC_TSEUG{{{` instead. If you share some more details, we may be able to suggest a better alternative. I assume that this is some kind of templating but I'm not sure. – Stefan Jun 28 '21 at 11:17
  • Thank you all, so here is the detail : the phrase i want to replace is on a template to modify messages to send to customers on content management page. I am trying to have it appear/disappear depending on whether the customer set a coupon or not. However i would only want the last occurence of the phrase to disappear, i.e if the phrase was in the middle of messages, i would want it to remain and only delete the last one which is currently automatically set in the end of the message. Sorry if my explanation is unclear ;(( Im happy to explain further details if this was not enough – hika Jun 28 '21 at 12:31

1 Answers1

0

Assuming the HTML is stored as string, I would do:

@company_page.html = @company_page.html.sub(/.*\K{{{GUEST_COUPON_DATE_LIMIT_LINE}}}/, '')

See this answer for more details

If you are working in a CMS and you want to allow the user to dynamically add content based on variables, you might want to check this gem

Rhet Prieto
  • 3
  • 1
  • 2