Ctrl-R
Ctrl-R

Reputation: 43

Sending an email notification on button click

I would like to create a button on any of my pin show pages that are available for loan/sale that, when clicked, sends an email to the owner of the pin letting them know that the current_user is interested in the item and then giving a notice to let the current_user know that the email was sent...

I have set up the whole email part and was able to get it to send an email on loading of the page, but I want it to only send when the button is clicked. It is looking like I will have to make the button load a page that both sends the email and displays confirmation. The problem I am having with that is getting the @pin and current_user variables to be passed into the page.

Am I going about this in the right way, or am I way off? Any help is greatly appreciated!

Here is how I am opening the send/confirm page:

<%= button_tag(:type => 'button', class: "btn btn-primary", onclick: "window.location.href='/sendrequest'") do %>
<%= content_tag(:strong, 'Request Contact') %>
<% end %>

And here is what I need to execute on that page:

<% if user_signed_in? %>
<% UserMailer.request_pin(@users, @pin).deliver %>
<p>
Your request has been sent!
</p>
<% else %>
...
<% end %>

All of the code in UserMailer.request_pin is working fine.

Upvotes: 0

Views: 3123

Answers (2)

Ctrl-R
Ctrl-R

Reputation: 43

Someone else has answered this for me on a different site, I was going about it in the wrong way. Here is the code:

/app/controllers/pins_controller.rb:

...  
def sendrequest  
  @user = current_user  
  @pin = Pin.find(params[:id])  #The culprit!  
  if user_signed_in?  
    UserMailer.request_pin(current_user, @pin).deliver  
    redirect_to @pin, notice: 'Request for contact sent.'  
  else  
  end  
end  
...  

/app/mailer/user_mailer.rb:

class UserMailer < ActionMailer::Base  
  default :from => "[email protected]"  

  def request_pin(user, pin)  
    @user = user  
    @pin = pin  
    mail(:to => "#{@pin.user.name} <#{@pin.user.email}>", :replyto => @user.email, :subject => "#{@user.name} has requested #{@pin.description}")  
  end  
end  

/app/pins/show.html.erb:

...  
<%= link_to "Request Contact", sendrequest_pin_path(current_user, @pin), class: "btn btn-primary" %>  
...  

/config/routes.rb:

...  
resources :pins do  
  resources :loans  
  member do  
    match 'sendrequest' => 'pins#sendrequest'  
  end  
end  
...

Upvotes: 3

user2255677
user2255677

Reputation: 1

You could make a separate controller action for notifying pin owners and have the link use jquery to submit the request then no page load is necessary and you can have the notification that it was sent be handled by the jquery as well.

I don't know your code base at all so this s a rough example

# Routs file
get "/pins/:id/post" => "pins#notify", as: notify_pin_owner

# Controller
def notify
@pin = Pin.find(params[:id])
<% if user_signed_in? %>
<% UserMailer.request_pin(@users, @pin).deliver %>
<p>
Your request has been sent!
</p>
<% else %>
...
<% end %>
end

# View
<%= link_to "notify", "#", data-link: notify_pin_owner_path( @pin ), class: 'pin-notification' %>

# In javascript
$(document).ready( function() {
  $('.pin-notification').click( function() {
    $.ajax({
      type: "GET",
      url: $(this).data('link'),
      success: 
    })
  })
})

Hope that helps

Upvotes: 0

Related Questions