views:

67

answers:

2

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

+1  A: 

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;
});
Darin Dimitrov
Yes but how does that connect to my rails controller? How do I set the vars for my Notifier?
Trip
Darin Dimitrov
Right. I got that part down. But it doesn't answer my question how these variables can be read by the rails controller. I appreciate the help nonetheless.
Trip
The rails controller could read them from the request.
Darin Dimitrov
It's giving me a 'doesn't know what those vars are' error. I assume I have to set them such as `email = foobar` . But how do I set it to something that's geting passed to it?
Trip
The js var is `email`, and I'm trying to send `email` as a var in the controller with `Notifier.deliver_email_blast(email, subject, body)` . Without saying the var, it says it doesn't know what `email`, etc. is. When I set `email = email` . It says I have a `nil object when i didn't expect it`. Any ideas?
Trip
They'll be in `params`, so `subject = params[:subject]`, just like normal requests to rails.
thenduks
Ah thanks @thenduks. Well, unfortunately it looks like those vars posted by @Darin don't pass through. Perhaps because its a "GET" and not a "POST" ?
Trip
Seems to me that'd be because he's got the `var subject = ...` line wrong, missing `.val()` :) No reason it shouldn't allow get... well, it would depend on your routes... Really you should just go ahead and make it a post, since that's what you're doing -- posting.
thenduks
So I changed both the AJAX request, and the Routes.rb to Post and I get `Only post requests are allowed.` . Whatever the heck that means :D
Trip
+1  A: 

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.

Peer Allan
The form below doesn't work. The vars don't pass to params[:subject], or any of the others. Is that because its a "GET" and not a "POST"? I tried both, also making sure that my route member is a :post request. When the routes.rb request and the ajax request is POST, then I get a `Only post requests are allowed`. when they are both GET, I get a nil param.
Trip
The controller is getting hit. I put a debugger right before when i define the params. But once I get there, they are all defined to `nil`. Strange right? I tried your serialize() method, and put a unique id around my form, and it doesn't give.
Trip
Where exactly is your debugger...after `def test_email` ?
tobym
It's right before the vars. Right after `def test_email` right before `user = params[:email]`
Trip
Could this have anything to do with my DTD? Or do I need this line anywhere ? `'beforeSend': function(xhr) {xhr.setRequestHeader("Accept", "text/javascript")}` . I noticed that in a RAilscast
Trip
That request header trick doesn't matter for actually seeing the params (it's to ensure that Rails knows the request is javascript, not html).
tobym
Look into changing that link_to into a link_to_function or button_to_function so it calls the JS more directly. Little bit of a guess at this point
Peer Allan
in your debugger, you can also take a glance at `controller.request.headers` and browse through that, especially REQUEST_URI, to see if the data is at least encoded. The params should definitely show up. What app server are you running when you see those results?
tobym
Wow! I got it! And I also realized I am retarded. After all is said and done, i didn't have my .js included.. can we just keep this between you guys and me? :)
Trip