1

I am working on Rails and having problem with ajax post data call. It is working and data is entered in db but the SUCCESS function is not working, as alert function in success function is not firing.

Further I want to add comment to my post without refreshing whole page.

AJAX code looks like :

<script type="text/javascript">
$(document).ready(function () {
    $('form').submit(function () {
        $.ajax({
            url: '/comments/create',
            type: "POST",
            dataType: 'json',
            processData: false,
            success: function (msg) {
                alert(msg);
            },
            error: function (xhr, status) {
                alert(xhr.error);
            }
        });
    });
});
</script>

and Controller code is :

def create
    puts 'params', params.inspect
    @post = Post.find(params[:post_id])
    @comment = @post.comments.new(params[:comment])
    respond_to do |format|
        if @comment.save
            format.html { redirect_to(@post, :notice => 'Thanks for comment.') }
            format.json { render json => @post, :msg=>"comment oK" }
        else
            format.html { render :action => "new" }
            format.json  { render :xml => @comment.errors, :msg=>"comment oooK", :status => :unprocessable_entity }
        end
    end
end

This code works fine to post the data to the create function, which creates (posts?) the entry to the db. But I would like to return the data in the alert function upon "success" but alert in success function is not working and alert in error function is firing.

3
  • What error does the error: return? Commented Jun 19, 2013 at 12:59
  • try these answer its working for me stackoverflow.com/questions/7237720/jquery-post-to-rails Commented Jun 20, 2013 at 21:51
  • Hi, I am new to rails may i know the process of how to store the data to database with ajax call in which .html.erb file i should write the call and what i need to define in routes. As i mentioned i am new to rails it will help me a lot. I have created a ajax call in new.html.erb but it is not working Commented Jul 9, 2014 at 8:01

1 Answer 1

1

Try:

 $('form').submit(function()
    {
     var myForm =  $('form').serialize();
        $.ajax
         ({
            url:'/comments/create',
             type:"POST",
             dataType:'json',
             data: myForm,
             processData:false,
            success: function (msg)
            {
            alert(msg);
            },
            error: function (xhr, status)
            {
            alert(xhr.error);
            }
         });
    });

If this doesn't work, please write what you're sending across the wire in your AJAX request. Use Chrome Console or Firebug to check and paste the POST results in here.

Sign up to request clarification or add additional context in comments.

1 Comment

this is not working. i m sending form data in my ajax request and data is inserted in db but success function is not executing

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.