0
votes

In my Rails 3.2 project, I have a form to create a new post in new.html.erb in app/views/posts/

<%= form_for(@post) do |post_form| %>
  ...
  <div class="field">
    <%= post_form.label :email %><br />
    <%= post_form.text_field :email %>
  </div>
  <div class="field">
    <%= post_form.label :title %><br />
    <%= post_form.text_field :title %>
  </div>
  <div class="field">
    <%= post_form.label :content %><br />
    <%= post_form.text_field :content %>
  </div>
  <div class="actions">
    <%= post_form.submit %>
  </div>
<% end %>

Then the create function in posts_controller.rb

def create
  @post = Post.new(params[:post])  
  if @post.save
    format.html { redirect_to @post }
  else
    format.html { render action: "new" }
  end
end

When the user submits a post, I want to send an email notification to email that "You have submitted a post with title title and content content." How can I do that?

3

3 Answers

0
votes

You'd set up a mailer, provide it with a template, and call the mailer action, either in the create action itself, or as a callback on the Post model.

Here's a Railscast on using ActionMailer in Rails 3. It should pretty much cover the steps.

0
votes

You would create a new mailer class and call this class from within your controller where you want the email to be sent. You can learn about ActionMailer here http://guides.rubyonrails.org/action_mailer_basics.html

0
votes

You'll have to make a mail setup initializer and a user_post_mailer.rb in the app/mailers/ folder

Then you can call

UserMailer.post_confirmation(@user).deliver

from you controller

Railscast mailer episode