New to rails!!!
How do i make my “Send” button call a function (sendmail) which sends an email?
This is what I have so far….
The sendmail in events_controller does not called!
And I have an event in my model, and i am running rails 3.2.3
show.html.erb:
<p><b>Send Results</b></p>
<%= form_tag("/events", :method => "get") do %>
<p><label for="email_recipient">Email recipient</label>:
<%= text_field 'email', 'Email recipient' %></p>
<%= submit_tag "Send" %>
<% end %>
routes.rb:
resources :events do
member do
get '/sendmail', :as => :sendmail
end
end
events_controller.rb
def sendmail
puts "sendmail"
Emailer.deliver_results_email
return if request.xhr?
render :text => 'Message sent successfully'
end
emailer.rb
class Emailer < ActionMailer::Base
#default from: "from@example.com"
def results_email
puts "RESULTS EMAIL"
#NEVER GETS THIS FAR.....
end
end
First things first. In your
routes.rbyou have put thesendmailaction inside amemberblock. This would generate a route like this:If the sendmail action is kindda generic and don’t need to be applied on a single ‘event’, maybe you should put it on a
collectionblock, so your route would be like this:Second, if you run
rake routes | grep sendmailin your console, you will get something like this outputed:What this line is saying is that you have a route configured and you have a helper route method named
sendmail_event_paththat you can use to facilitate the understanding of your views, etc. So in your view it would be something like this:When you click the submit button the form would be
POSTEDto the sendmail action of yourevents_controller.I suggest you to change your route to a
collection, and change to:post, so you can use it without the event isntance.