I have an email form. But I'm making a test email form where a user can add a unique email, and have the email test send it to that particular email.
To make it easy, I decided to make the test email perform via ajax, and stick the whole thing inside of the other email form.
I can't figure out how to send the variables from my HAML to my controller
new.html.haml
- form_tag admin_email_blast_path do
Subject
%br
= text_field_tag 'subject', :class => "mass_email_subject"
%br
Body
%br
= text_area_tag 'message', '', :name => 'editor1', :class => "mass_email_body"
%br/
= submit_tag 'Send Email', :class => 'button'
.grid_3.right.align_right.suffix_1 # <= TEST EMAIL :D
= text_field_tag 'email', nil, :style => "width: 150px;", :class => "test_email_address"
= link_to 'Test Email', test_email_admin_email_blast_path, :class => 'button test_email'
JS
$(".test_email").live("click", function() {
var email = $(".test_email_address")
var subject = $(".mass_email_subject");
var body = $(".mass_email_body");
data = "email=" + email.val() + "&subject" + subject.val() + "&body" + body.val();
$.ajax({type: "GET",
url: $(this).attr("href"),
dataType: "script"
data: data
});
return false;
});
Controller
def test_email
debugger
email = params[:email]
subject = params[:subject]
body = params[:body]
Notifier.deliver_email_blast(email, subject, body)
end
routes.rb
admin.resource :email_blast, :member => {
:test_email => :get
}
I apologize ahead of time if this is a dumb newbie question. :(
Create target "JSON object Mapper" object class file according to the business requirements. Create a "Controllers\HomeController. cs" file with default Index method and GetData(...) method with string type input query parameters for Ajax call with following lines of code i.e.
What About jQuery and AJAX? jQuery provides several methods for AJAX functionality. With the jQuery AJAX methods, you can request text, HTML, XML, or JSON from a remote server using both HTTP Get and HTTP Post - And you can load the external data directly into the selected HTML elements of your web page!
You could use the data
parameter:
$('.test_email').live('click', function() {
var subject = $('.mass_email_subject');
var body = $('.mass_email_body');
$.ajax({
type: 'GET',
url: this.href,
dataType: 'script',
data: { subject: subject.val(), body: body.val() }
});
return false;
});
Assuming you are getting the submit to work correctly based on Darin Dimitrov answer then you need to use the params object. In this case it looks like all you need to do is access params[:subject] and params[:body] in your controller action.
EDIT:
In response to the comment, GET and POST make no difference to params. If the form is not working, try modifying you JS to use serialize and return an error on failure:
$(".test_email").live("click", function() {
var subject = $(".mass_email_subject")
var body = $(".mass_email_body")
$.ajax({type: "GET",
url: $(this).attr("href"),
dataType: "script"
data: $('#form_id').serialize(),
error: function(){ alert("There was a problem, please try again.") }
});
return false;
});
If it still is not working then you better look to see if your controller action is being hit at all.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With