I am relatively new to Ruby on Rails and so I am finding myself stuck
on an issue for a web site I am currently designing.
I would like to create a simple mailing list form that just sends the
information from the form fields straight to an email account. I want
this form to be inside of a larger application which is much like a
blog. What is the best way of going about this?
Rails has a built in mailing mechanism known as ActionMailer… there are TONS of good resources on setting this up correctly, and I’ll leave that part to our friend Google.
Basically, you can do this two ways: set up this form, and have it post to an action that takes the information, sends it over to your already set up action mailer, and then redirects you to a page telling you your mail was succesfully sent. However, if you don’t want to interrupt the flow of your program to much, this is an almost perfect use case for getting into some AJAX. Simply have the submit button on your form use AJAX instead of a regular submit, and it will do all the mailing in the background, and then come back and tell your page that it is done mailing.
If you’re talking about having the same form perform more than one action (such as, when they click on post to post a blog entry, it also sends an email), it is simple to have your action you are sending the blog post information to also create an email out of what you have sent… in the application I’m currently working on, we use the wonderful Rails method render_to_string to accomplish this, as that allows us to even use a view, layout, etc just like normal, and pass the resulting nicely formatted HTML stuff off to an action mailer.