Hello, I want to implement ajax voting on the site using the acts_as_votable heme. There is such code:

def upvote @answer.upvote_from current_user, :vote_weight => 1 respond_to do |format| format.js end end 

In the views / answers folder I added the upvote.js.erb file and for a start I wrote a simple console.log there. But instead of displaying a message in the console, I get an error ActionController :: UnknownFormat. Please tell me how to fix? View code for response:

  = link_to like_answer_path(answer), class: "like", method: :put do button.btn.btn-info aria-label=("Left Align") type="button" span.glyphicon.glyphicon-thumbs-up.glyphicon-align-center aria-hidden="true" span.badge= answer.get_upvotes.size = link_to unlike_answer_path(answer), class: "like", method: :put do button.btn.btn-info aria-label=("Left Align") type="button" span.glyphicon.glyphicon-thumbs-down.glyphicon-align-center aria-hidden="true" span.badge= answer.get_downvotes.size 
  • one
    This error means that the respond request from respond_to has nothing to answer. The reason for the error is the request sent. Attach it. - D-side
  • @ D-side Hmm, the code associated with the upvote method I indicated, in upvote.js.erb, alert ("hello") is currently written. What else is needed? - Maxim Cherevatov pm
  • The fact that this method is knocking. - D-side
  • I do not understand (I added the view code. Before the upvote method, before_action is triggered, but there it’s just finding the answer by id -> @answer = Answer.find (params [: id]) - Maxim Cherevatov
  • A lot of superfluous, enough links to "like". Yes, naturally, we already have a respond_to, everything works before it. - D-side

1 answer 1

tl; dr: in the link_to call for the link_to not enough remote: true .

The link makes a normal (not AJAX) request on behalf of a browser that needs a web page or, at worst, text to open the content in the current window / tab. But you can clarify this by opening the request made by the inspector (or pry !) For the Accept header.

The browser in such requests does not need a JS response. You will not see the corresponding MIME type in this header. But your respond_to written so that it only serves requests that request JS!

It looks like an unfinished attempt to apply :remote (an epic crutch, unfortunately, being part of Rails), which makes the aforementioned request with the intention of getting the JS executable from the server to change the currently loaded page. Only the very beginning is forgotten.

  • Aga, thank you) For some reason I decided that remote: true should only be written for forms. - Maxim Cherevatov pm