Sending email via a notification button

Asked

Viewed 402 times

7

I wonder if you have done or have any example that can send the link to the following problem:

I have a project that has the Books crud and a User controller with their respective views. I made the regular e-mail module like the generate mailer which 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 book loan.

Thanks in advance for your attention.

  • 1

    I think you will have to create in your template (Models) a method that calling the Mailer method already created.

  • 1

    @augustoppimenta as well as the models, the Mailer has already been created! What I want is when the user presses a button, send an email!

  • 1

    Take a look at that link. I think it might help.

  • 1

    @augustoppimenta this has already been done! There is an email notification when a user is created! What I was wanting is that after the logged in user wants to press a 'Loan' button of a book' the user who posted the book can receive an email with the request!

3 answers

2


Come on. I will try to be as soon as possible. First you did not specify whether the user who will ask for the book is logged in or not. I’ll assume he is, but if he wasn’t, you’d have to pass some of his contact parameters. I’m also going to assume that your Mailer has a book_loan_request(user_email, book_id) action that receives the email from the requested user and the book id.

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
  • Thank you so much for your help, you helped me so much! It’s been solved! Thanks

2

What you need Adriano is an action in the Controller that is triggered by clicking 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, ex, in the file Routes.Rb

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

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

That’s been simplified, but that’s the idea.

  • you’re talking to create this method in the Books controller or create a new controller !?

  • can be in your own controller Books. and creates a route for the action

  • Your route can be improved tbm. You can use Resources resources :books do member do post :book_loan_request end end .. this would become the url : /Books/123/book_loan_request

1

Take a specific route for this action.

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

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.