1

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.

6
  • 5
    do you want it to respond with js or json? (because you're asking for the latter, but expecting it to return the former)? Commented Jul 24, 2014 at 4:53
  • I'd like it to respond with js. So, I would change the url to .js, datatype to js, etc. to reformat the POST request as a JS call? Commented Jul 24, 2014 at 4:55
  • I changed the url, dataType, and contentType and now I'm getting a response with render js: "console.log('test');", but a MissingTemplate error is being thrown when I just have format.js or format.js {} Commented Jul 24, 2014 at 5:07
  • Is your template in the right directory? In the error message Rails tells you where he looked for it. Check if your js template is in one of them. Commented Jul 24, 2014 at 5:12
  • 1
    See github.com/rails/coffee-rails/issues/36 for more info about the issues with .erb. Commented Jul 24, 2014 at 5:26

2 Answers 2

2

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

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

Comments

1

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

1 Comment

Thanks for the info. My confusion was not recognizing that a url with .json would return json. For some reason, I thought because it was a POST request the json referred to the date being post not what the server would respond with. All is clear now. Thanks!

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.