tardjo
tardjo

Reputation: 1654

jquery post get missing template in rails

in my jquery post I have code like this

function checkBill(trs_id, bill_id){
    $.ajax({
      type: 'POST',
      url: "/check_bill",
      data: {bill_id: bill_id, transaction_id: trs_id},
      headers: {
        'X-CSRF-Token': '<%= form_authenticity_token.to_s %>'
      },
      dataType: 'script'
    });
  }

in my controller successfully get params for jquery post, but I get error missing template in my log? I don't know why? I guess jquery post not need template

Upvotes: 1

Views: 219

Answers (2)

Richard Peck
Richard Peck

Reputation: 76774

The missing template is basically a lack of check_bill.js.erb in your views folder


Views & Ajax

When your server receives a request (either HTTP or Ajax), it will process it by running the action & rendering the required view

Rails allows you to render HTML & JS views, which come in the form action_name.html.erb or action_name.js.erb. If you're sending a JS request, Rails therefore tries to render the .js.erb


Returning JS Requests

If you want to return JS requests, you'll need to use the code the other answers have stated (respond_to):

#app/controllers/your_controller.rb
def check_bill
    respond_to do |format|
        format.js { render :nothing => true } 
    end
end

The difference is what you want to return to Ajax. I'll update my answer if you let us know what kind of response you're expecting

Upvotes: 1

sissy
sissy

Reputation: 2988

you should give instructions to your controller action to respond with a js. Like this:

 respond_to do |format|
   format.js {render :text => 'ok'}
 end

otherwise it will try to reply with an html view

Upvotes: 0

Related Questions