简体   繁体   中英

Why does rails not respond to the ajax post request with javascript?

I have a button with a click even handler:

score_button_click_handler = (e) ->
$.ajax
      url: "/practice_scores/#{practice_score_id}.json",
      dataType: "json",
      type: "POST",
      contentType: "application/json",
      processData: false,
      data: "{ \"practice_score\": {\"score_id\": #{score_id}, \"practice_id\": #{practice_id} }}",
      beforeSend: (xhr) ->
        xhr.setRequestHeader("X-Http-Method-Override", "PUT");
      success: ->
        category_id = $("#category-practices").data("category-id")
        assessment_id = $("#viz").data("assessment-id")
        render_average assessment_id, category_id

In practice_scores_controller.rb , I have the action:

def update
    @practice_score = PracticeScore.find(params[:id])

    respond_to do |format|
      if @practice_score.update_attributes(params[:practice_score])
        format.html { redirect_to questionnaire_path, notice: 'Practice score was successfully updated.' }
        format.json {render "update.js.coffee.erb"}
      else
        format.html { render action: "edit" }
        format.json { render json: @practice_score.errors, status: :unprocessable_entity }
      end
    end
  end

In app/views/practice_scores/ is update.js.coffee.erb . It has a simple console log statement in it.

I can't for the life of me get the update view to render. I have also tried the following just to see if I can get it to respond.

format.json {render js: "alert('Something')"}

I'm not seeing any thing in the console or having an alert pop up. Ultimately, I'd like to respond to the request with coffee/js to update my UI. Does it have to do with the POST request? I have scoured the blogs and I seem to be doing things they way they describe, but I'm just not seeing a response.

url: "/practice_scores/#{practice_score_id}.json",

From the above, you are making an ajax call to a .json endpoint, which leads me to believe you were wanting json as a response, but you are not. You probably want a regular url like /practice_scores/#{practice_score_id} .

Assuming you have the route defined properly.......

I would test that render "update.js.coffee.erb" is working on its own, removing the entire respond_to do |format| block.

After you determine that it works...

You would definitely not be doing format.json , but format.js

JSON

To add to Sherwyn Goh 's answer, you need to appreciate what JSON is for - it's a notation system to help you output / use data in your application

If you submit a JSON request to your app, you'd expect a JSON-encoded response of data, not a pure JS response. If you want a pure JS response, just send the request as standard javascript

--

respond_to

You may benefit from reading up on the respond_to mechanism of Rails, and the different mime types on the Internet in general.

Specifically, if you want to bring back pure data (in JSON notation), you'll want to send a json request. IF you want to cause a piece of functionality to occur, you'll want to send a pure js request:

#app/assets/javascripts/application.js
score_button_click_handler = (e) ->
$.ajax
  url: "/practice_scores/#{practice_score_id}"


#app/controllers/practice_scores_controller.rb
Class PracticeScoresController < ApplicationController
   def update
       ...
       respond_to do |format| 
          format.html
          format.js #-> renders /views/practice_scores/update.js.erb
       end
   end
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM