user2759575
user2759575

Reputation: 553

Post link using jquery

Using this post as reference, i'm trying to post links using ajax (which i'm totally new to so please bear with me). At this point, nothing happens when i click the 'post' button.

My form looks like this:

<%= simple_form_for [question, Link.new], :remote => true, :id => 'create_item' do |f| %>
<%= f.input :body %>
<%= f.button :submit, "Post", :onclick=>"javascript:submitForm()", class: "button" %>

and the jquery looks like this:

<script type="text/javascript">

function submitForm() {
    $.ajax({
        type:'POST', 
        url: 'questions/:question_id/links/new',
        data:$('#create_item').serialize(), 
        success: function(response) {
            $('#create_item').find('#item').html(response);
        }
    });

    return false;
}

</script>  

Upvotes: 0

Views: 242

Answers (1)

Barmar
Barmar

Reputation: 781340

Change to:

:onclick => "return submitForm()"

The click handler needs to return false to prevent the default submission. Your submitForm function returns false, but you weren't returning that from the handler.

Also, you don't need javascript: in onXXX attributes. That's only needed in attributes that contain URIs, like href and src, to tell them to run the script instead of accessing a URL.

Upvotes: 1

Related Questions