Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 896667
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T14:45:50+00:00 2026-05-15T14:45:50+00:00

I have several controllers that take an instance of different classes each (Email, Call,

  • 0

I have several controllers that take an instance of different classes each (Email, Call, Letter, etc) and they all have to go through this same substitution:

@email.message.gsub!("{FirstName}", @contact.first_name)
@email.message.gsub!("{Company}", @contact.company_name) 
@email.message.gsub!("{Colleagues}", @colleagues.to_sentence)
@email.message.gsub!("{NextWeek}", (Date.today + 7.days).strftime("%A, %B %d"))
@email.message.gsub!("{ContactTitle}", @contact.title )

So, for example, @call.message for Call, @letter.message for Letter, etcetera.

This isn’t very dry. I tried the following:

class ApplicationController < ActionController::Base
  helper :all # include all helpers, all the time

  def message_sub(asset, contact, colleagues)
    asset.message.gsub!("{FirstName}", contact.first_name)
    asset.message.gsub!("{Company}", contact.company_name) 
    asset.message.gsub!("{Colleagues}", colleagues.to_sentence)
    asset.message.gsub!("{NextWeek}", (Date.today + 7.days).strftime("%A, %B %d"))
    asset.message.gsub!("{ContactTitle}", contact.title )
  end
end

So in, say, the Letter Controller have this:

@letter = Letter.find(params[:letter]) #:letter was passed as a hash of the letter instance

message_sub(@letter, @contact, @colleagues)

@contact_letter.body = @letter.body

But the above doesn’t work.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T14:45:51+00:00Added an answer on May 15, 2026 at 2:45 pm

    To answer your question directly, you want a module. You can put it in your lib directory.

    module MessageSubstitution
    
      def message_sub(asset, contact, colleagues)
        asset.message.gsub!("{FirstName}", contact.first_name)
        asset.message.gsub!("{Company}", contact.company_name) 
        asset.message.gsub!("{Colleagues}", colleagues.to_sentence)
        asset.message.gsub!("{NextWeek}", (Date.today + 7.days).strftime("%A, %B %d"))
        asset.message.gsub!("{ContactTitle}", contact.title )
      end
    
    end
    

    Then in your controller

    class MyController < ApplicationController
    
      include MessageSubstitution
    
    
      def action
    
        ...
    
        message_sub(@letter, @contact, @colleagues)
    
      end
    
    end
    

    However, as some have already pointed out, this is probably better at the model level:

    module MessageSubstitution
    
      def substituted_message(contact, colleagues)
        message.gsub("{FirstName}", contact.first_name).
               gsub("{Company}", contact.company_name).
               gsub("{Colleagues}", colleagues.to_sentence).
               gsub("{NextWeek}", (Date.today + 7.days).strftime("%A, %B %d")).
               gsub("{ContactTitle}", contact.title )
      end
    
    end
    

    Which you would then include at the model level, and then call in your controller like @letter.substituted_message(@contact, @colleagues)

    However, I’m a little confused that what you posted didn’t work, though. If it’s defined on ApplicationController it should work. Module-based solution still better IMO.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.