Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby Blank Slates in Ruby on Rails Email with Rails Adding a Mailer Method

Why am I getting the failure notice?

I don't understand why I am getting the failure notice when i try to send the email?

controller

def email_profile
    @profile = Profile.find(params[:profile_id])
    destination = params[:to]
    share = Share.profile(@profile, destination)
    if destination =~ /@/ && share.deliver
      redirect_to @profile, notice: 'email sent'
    else 
      redirect_to @profile, notice: 'email failed'
    end
end

view

<button class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">
  Email
</button>

<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
        <h4 class="modal-title" id="myModalLabel">Share my Info with someone</h4>
      </div>
      <div class="modal-body">
        <%= form_tag profile_email_profile_path(@profile), method: :put do %>
          <%= label_tag :destination, "What email address would you like to send this to?" %>
          <%= text_field_tag :destination %>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
        <%= submit_tag "Send Email", class: "btn btn-primary" %>
        <% end %>
      </div>
    </div>
  </div>
</div>

mailer

class Share < ActionMailer::Base
    default_url_options[:host] = "localhost:3000"
  default from: "from@example.com"

  def profile(profile, destination)
   @profile = profile
   mail(to: destination, subject: "sent you stuff")
  end
end

terminal

Started PUT "/profiles/1/email_profile" for 127.0.0.1 at 2014-08-29 12:12:44 +1000
Processing by ProfilesController#email_profile as HTML
  Parameters: {"utf8"=>"āœ“", "authenticity_token"=>"lNgA2/SNZCifVx33Hp53YAQxRPRW7lRsHsJuETSNKgE=", "destination"=>"joe.smith@gmail.com", "commit"=>"Send Email", "profile_id"=>"1"}
  User Load (0.2ms)  SELECT  "users".* FROM "users"  WHERE "users"."id" = 1  ORDER BY "users"."id" ASC LIMIT 1
  Profile Load (0.1ms)  SELECT  "profiles".* FROM "profiles"  WHERE "profiles"."id" = ? LIMIT 1  [["id", 1]]
  Rendered share/profile.html.erb (0.3ms)
  Rendered share/profile.text.erb (0.2ms)

Share#profile: processed outbound mail in 11.0ms
Redirected to http://localhost:3000/profiles/1
Completed 302 Found in 16ms (ActiveRecord: 0.3ms)

Started GET "/profiles/1" for 127.0.0.1 at 2014-08-29 12:12:44 +1000
Processing by ProfilesController#show as HTML
  Parameters: {"id"=>"1"}
  User Load (0.1ms)  SELECT  "users".* FROM "users"  WHERE "users"."id" = 1  ORDER BY "users"."id" ASC LIMIT 1
  Profile Load (0.1ms)  SELECT  "profiles".* FROM "profiles"  WHERE "profiles"."id" = ? LIMIT 1  [["id", 1]]
  Rendered profiles/show.html.erb within layouts/application (0.9ms)
  Profile Load (0.1ms)  SELECT  "profiles".* FROM "profiles"  WHERE "profiles"."user_id" = ? LIMIT 1  [["user_id", 1]]
Completed 200 OK in 10ms (Views: 8.8ms | ActiveRecord: 0.3ms)

profile.html.erb

<p>sent you their information.</p>

profile.text.erb

sent you their information.

environment/development

# Don't care if the mailer can't send.
  config.action_mailer.raise_delivery_errors = true

  config.action_mailer.delivery_method = :smtp

  # Gmail SMTP server setup
  ActionMailer::Base.smtp_settings = {
    :address => "smtp.gmail.com",
    :enable_starttls_auto => true,
    :port => 587,
    :authentication => :plain,
    :user_name => "joe.smith@gmail.com",
    :password => '46292839'
  }
# Raises error for missing translations
  # config.action_view.raise_on_missing_translations = true
  config.action_mailer.default_url_options = { :host => 'localhost:3000' }

  # false prevents mail from being sent in development environment
  config.action_mailer.perform_deliveries = true

probably also good to note that I have included everything here that i have set up related to sending email.

Maciej Czuchnowski
Maciej Czuchnowski
36,441 Points

Please use markdown to make this code readable (see Markdown Cheatsheet below).

I tried - I couldn't get the ''' 's to work? Ok I've tried several times actually. Any tips I really cant work it out.

David Moore
David Moore
13,916 Points

Put the backticks on a new line and be sure it's a backtick (key above tab on most keyboards) not a ' (a single quote).

Edit: Dang thats a pain to describe! Every time use the character, markdown wants to interpolate it into something.

Maciej Czuchnowski
Maciej Czuchnowski
36,441 Points

Just copy them from the cheatsheet.

Maciej Czuchnowski
Maciej Czuchnowski
36,441 Points

Also, put the three backticks in separate lines. Don't put any text before or after them in the same line, just like the cheatsheet shows you.

Hey guys, I tried all of those suggestions multiple times when I posted the question. I watched the 'tips for asking questions' and all because I knew it didn't look good. still dont know how to fix it if I've tried those..

EDIT: worked it out! I was doing it all right but I had headings 'controller:' on the line directly above the line. The line needed a gap above it (even though it was on its own line) otherwise the heading interfered with it. weird