0
votes

New with javascript and Rails. I've gotten some simple jQuery to work with my rails app when it's not a form.

Now I'm trying to do jQuery actions when the user hits 'Submit' in the form. The issue I have is that HTML code is executed instead of the scripts. BTW: Used scaffolding to create a simple form. It's the CREATE submit action I want to fetch.


application.js:

jQuery.ajaxSetup({
  'beforeSend': function(xhr) {
    xhr.setRequestHeader("Accept", "text/javascript")
  }
})

$(document).ready(function() {

  $("#new_travel").submit(function() {
    $.post(this.action, $(this).serialize(), null, "script");
    return false;
  });

create.js.erb:

alert('Yeah!');  // Have more, but just want to see the jQuery to be executed

new.html.erb:

<%= form_for(@travel, :remote => true) do |f| %>
..
..
  <div class="actions">
    <%= f.submit %>

The travels controller holds the format.js in the respond_to block

Any clues as to what's wrong? Let me know if any information is missing.

Thanks!

1
$("#new_travel").submit(function() { $.post(this.action, $(this).serialize(), null, "script"); return false; }); : is this: this.action a valid jq/js statement..?shybovycha
Yep, But the formatting for my post here could be better. :).TheBlot

1 Answers

0
votes

SOLVED!

I found out the it did actually work for standard forms, but as the form I operated on was shows inside another page as part of another Jquery call the $("#new_travel") did not detect it.

I had to change that to: $("#new_travel").live('submit' function()...

And then it worked as all following #new_travel on the page got the javascript 'hook'.