Reputation: 3806
I have a link_to form helper, that has an id:
<%= link_to "NEXT", next_wizard_path, :method => :put, :id => 'my-link-check' %>
I want this link to to submit the form that is on the page (its actually a partial but that shouldn't matter its all one page when rendered). So I put in some jquery on the top of the header partial:
<script>
$(document).ready(function() {
$('#my-link-check').click(function(event){
var form = document.getElementById("edit_put");
if(form!=null)
form.submit();
//event.preventDefault(); // Prevent link from following its href, dont think we want this
});
});
</script>
In an odd twist the form.submit does indeed work, IF i am in firebug with a breakpoints set and specifically step through the code, if i do not, it runs the code but the submit does not actually work. I am so confused as to what is going on, or why it would be behaving like this. I suspect its in how I am using the jquery call as I am not well versed in jquery at all.
I should note, this is to save data on a form that is being served and helped with wicked, which is this wizard gem for ruby on rails.
I should also note the author of wicked suggested to save data between "wizard pages" to submit data asynchronously whenever the form is touched via ajax:
def update
# save logic here
respond_to do |format|
format.js do
# whatever
end
format.html do
render_wizard
end
end
though I have no clue, is that making form_for in our view remote => true ? I suspect it is more involved than that.
I also tried having the link_to method be a put method, this did call the Update action in the controller, but it passed no information so it was not saving any form changes...its why I thought the submit action call thru javascript was a good solution, apparently I was mistaken.
Upvotes: 1
Views: 150
Reputation: 12818
Why have you commented out event.preventDefault();
line? Does the link with my-link-check
id lead to actual address?
Looks like the browser just changed (or reload) the page, and script execution is terminated. With break point everything goes OK.
Try to uncomment event.preventDefault();
or put return false;
after form.submit();
Upvotes: 1
Reputation: 2878
I suspect that jQuery submit
only works on a jQuery object, so it should be something like this:
$(document).ready(function() {
$('#my-link-check').click(function(event){
var form = $("#edit_put");
if(form!=null)
$(form).submit();
});
});
Upvotes: 0