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 6055245
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T08:14:37+00:00 2026-05-23T08:14:37+00:00

About a year ago I decided to make sure that every flash notice that

  • 0

About a year ago I decided to make sure that every flash notice that had text that wasn’t unique would get the text from a method in a module. My initial reason for doing this was to avoid typing out the same string over and over. If I wanted to change the wording I could do so easily in one spot and the likelihood of typos from repeating the same thing over and over would be decreased.

What I ended up with was this:

module Messages
    def format_error_messages(errors)
        errors.map {|attribute, message| "Error: #{attribute.to_s.titleize} #{message}.<br />"}
    end

    def error_message_could_not_find(object_name)
        "Error: Unable to find the specified " + object_name + "!"
    end

    def error_message_could_not_create(object_name)
        "Error: Unable to create the " + object_name + "!"
    end

    def error_message_could_not_save(object_name)
        "Error: Unable to save " + object_name + " to database!"
    end

    def error_message_could_not_update(object_name)
        "Error: Unable to update " + object_name + "!"
    end

    def error_message_could_not_destory(object_name)
        "Error: Unable to destroy " + object_name + "!"
    end

    def notice_message_created(object_name)
        object_name.capitalize + " has been created!"
    end

    def notice_message_updated(object_name)
        object_name.capitalize + " has been updated!"
    end

    def notice_message_destroyed(object_name)
        object_name.capitalize + " has been deleted!"
    end
end

In my controllers when I’m setting up the flash I can do something like:

flash[:notice] = notice_message_created("post")

That way all successfully created objects generate similar messages.

I’m redoing one of my projects now and am going over all of this and I have this nagging feeling that this is just not the best way to go about doing this. I mean, it works, and it’s served me well so far, but I’ve never seen anyone else do this and I’m starting to think there’s a reason. Having to add a new method every time I need a new string seems almost silly. But then how else would I go about being able to insert contextual text into the string (like the name of the type of object we’re dealing with)?

Is there a Rails community standard when it comes to dealing with common strings of text? Especially ones that need to have additional text inserted into them at runtime?

I’ve been reading up on Rails’ internationalization since it would be nice to have my projects localized into more than one language and I like the idea of having all my strings of text in nice YAML files which would kill two birds with one stone. But I don’t see how I could do that and keep the same sort of functionality I’ve already got and need (unless I’m horribly mistaken and I don’t need it because there’s a better way).

I’m pretty much open to any ideas, suggestions, or relevant reading on this issue.

  • 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-23T08:14:38+00:00Added an answer on May 23, 2026 at 8:14 am

    You could use as an alternative approach the Rails Internationalization API. Then you could fill your locale file with the strings you want to use. Any parameter you want to give can be included like in the example ins section 4.2 in the tutorial.

    I18n.backend.store_translations :en, :thanks => 'Thanks %{name}!'
    I18n.translate :thanks, :name => 'Jeremy'
    # => 'Thanks Jeremy!'
    

    So do the following steps:

    1. Visit the file config/locales/en.yml

    2. Insert there your localization:

      en:
      created: “#{name} has been created!”

    3. Call instead of notice_message_created(object_name) the following: t(:created, object_name.capitalize)

    At the end, everything should work as before, you have get rid of a lot of simple methods, and you are able to internationalize your application by including the locale, and adding locale files to config/locales.

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

Sidebar

Related Questions

About a year ago someone helped me to setup a file that would pipe
A similar question was asked about a year ago, and wasn't quite resolved, but
About half year ago I decided to improve my programming efficiency, so I learned
about a year ago I stumbled across a nice feature in Java that I
This was asked about a year ago , but never really answered. It would
I've got a website that was created about an year ago and its been
About a year ago, a manager in another department brainstormed that I could code
About a year ago we are developed for internal use inprocess MOLAP engine that
I used SPRY about a year ago to experiment with the fact that you
Below is a function I found about a year ago that is supposed to

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.