Sending e-mail via a notification button

7

I would like to know if you have already done or have an example that can link to the following problem:

I have a project that has the crud of books and a User controller with their respective views. I made the normal email module as the generate mailer that is called loan.

I would like to know how to put a button with a link in the view book that when clicking, send an email requesting the loan of the book.

Thank you in advance for your attention.

    
asked by anonymous 05.11.2015 / 00:13

3 answers

2

Come on. I'll try to be as brief as possible. First you did not specify whether the user who will order the book is logged in or not. I'll assume he is, but if he was not you would have to pass some contact parameter on him. I will also assume that your mailer has a book_loan_request (user_email, book_id) action that receives the email of the user who requested it and the id of the book.

Follow the action:

    class BooksController
      def book_loan_request
        LoanMailer.book_loan_request(current_user.email, params[:id]).deliver_now
        redirect_to book_path(params[:id])
      end
    end

And the route:

    resources :books do
      member do
        get :book_loan_request
      end
    end
    
18.11.2015 / 04:00
2

What you need Adriano is an action on the Controller that is triggered by the click of the button through a URL.

Example:

Your controller would have an action:

def send_invite_book
   seu_parametro = params[:seu_parametro]
   SeuMailer.alguma_funcao(seu_parametro).deliver_now

   # Aqui você pode usar redirect, respond_to para html, js, json, etc.
end

In your route, you need to have access to this function, eg, in the routes.rb file

post '/url_que_vc_quer', to: 'seuController#send_invite_book'

Now, if you put this url in your view using the path helpers for example, you will call the action on the controller that will trigger the email calling your Mailer.

This is simplified, but the idea is this.

    
17.11.2015 / 15:50
1

Make a specific route for this action.

In the controller, implement the action that responds to this route.

In this action, call the method in your Mailer that sends the email you want

    
17.11.2015 / 15:49