Trip
Trip

Reputation: 27114

How does one pass AJAX vars from jQuery to their controller?

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. :(

Upvotes: 5

Views: 5252

Answers (2)

Peer Allan
Peer Allan

Reputation: 4004

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.

Upvotes: 3

Darin Dimitrov
Darin Dimitrov

Reputation: 1038710

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;
});

Upvotes: 7

Related Questions