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

  • SEARCH
  • Home
  • 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 3212408
In Process

The Archive Base Latest Questions

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

For my Rails app, we developed an half home-brewed email system. We created a

  • 0

For my Rails app, we developed an half home-brewed email system. We created a model called Email which then gets added to a queue to be sent (using a web service).

To create templates, we’ve been just mashing strings together in the model, ie:

Email < ActiveRecord::Base

   def self.stock_message(recipient)
     email = Email.create do |e|
       e.recipient = recipient
       e.message = String.new
       e.message << "first line <br />"
       e.message << "second line <br />"
     end 
   end

end

#to send:
e = Email.stock_message "foo@bar.baz"

This clearly violates MVC and really becomes a problem when I want to format strings using helper methods. How can I properly separate the view code from the model?

  • 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-17T14:50:08+00:00Added an answer on May 17, 2026 at 2:50 pm

    I think you can use render_to_string method, but as it is not availble in model, you need to call it from controller:

    # model
    Email < ActiveRecord::Base
      def self.stock_message(recipient, message)
        email = Email.create do |e|
          e.recipient = recipient
          e.message = message
        end 
      end
    end
    
    # controller
    e = Email.stock_message "foo@bar.baz", render_to_string "email/my_email"
    

    If you need to pass some variables to render method, then just add :locals => {:var1 => value, :var2 => value2}.

    # view
    first line <br />
    second line <br />
    

    In this example you should store views for emails in app/views/emails/ directory.

    However, as @David said, why not use mailer provided by Rails? It would be better solution.

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

Sidebar

Related Questions

Which version control system should I use for a Rails app: Git or SVN?
Simple rails app: I have 2 models, user and intro [which is simply a
My Rails app has some pages which are SSL-required and others which are SSL-optional.
i've developed small web app for personal use with Ruby on Rails. Now when
I have a Rails app that I developed on Windows. Now I'm bringing it
I've got an app developed on Ruby 1.8.6 and frozen to use Rails 2.0.2
I'm developing a rails app which handles a public area (as of today, static
I've developed a rails website in which I've got a text box with autocompletion
my app is developed from 2 parts - rails frontend and java backend. Backend
I have a Rails 3.0 app which is using a normal :remote => true

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.