mercredi 6 avril 2016

Add attachment to email using action Mailer - Rails 4

I have book model which has title, page_no and description. There is another model Image.

class Book < ActiveRecord::Base
    has_many :images, dependent: :destroy
end 

class Image < ActiveRecord::Base
  belongs_to :book
end

I have used paperclips to upload the images.I need to create a button in book's show page which should send all the book's attributes along with images in email.For this I have configured the action mailer as:

rails g mailer BookMailer

and created app/views/book_mailer/booklist_email.html.erb

<!DOCTYPE html>
<html>
  <head>
    <meta content='text/html; charset=UTF-8' http-equiv='Content-Type' />
  </head>
  <body>
    <h1>Welcome to Student Library</h1>
    <p>
      Book: <%= @book.title %>
    </p>
    <p>
      Page Number: <%= @book.page_number %>
    </p>
    <p>
      Description <%= @book.description %>
    </p>
    <p>
      Active <%= @book.active %>
    </p>
    <p>
      Please visit us again !!
    </p>
  </body>
</html>

Also I placed button in book's show page as

<%= content_tag(:button, "Send Email", :type=>:submit)%>

Now how should I trigger the email after clicking button from book show pages which should send email to any user where email should be given at the time of pressing send email button from book show page. Let me know if I need to provide more information. I am confused how should I proceed. Please help

Aucun commentaire:

Enregistrer un commentaire