Handling controller actions as JS instead of HTML

So, I have the following form:

<%= form_tag url_for(:controller => 'profile', :action => 'follow_topic'), :remote => true do %> <%= hidden_field_tag :topic_id, topic_id %> <%= content_tag :button, :class => 'link', :onclick => "javascript:document.getElementById('followtopic#{Topic.find(topic_id).identifier}').innerHTML='Following...'" do %> Follow <% end %> <% end %> 

and I'm trying to get the controller to treat it as JS instead of HTML. The funny thing is that I have a form like this in another place of the application, which seems to work fine, and the controller definitions are the same. I can not understand the problem. Any ideas on what I should check first?

 def follow_topic @topic = Topic.find(params[:topic_id]) current_user.follows << @topic respond_to do |format| format.js end end 
+4
source share
2 answers

You understand that your format.js does nothing. What do you expect in your form? and what do you expect in return? json or http 200 answer?

specify this in format.js as follows:

 ... respond_to do |format| format.js { render :nothing => true, :response => :ok if current_user.follows << @topic } end ... 

or something like that.

0
source

This question is OLD, but it did not answer, and I just struggled with the same problem for several hours, so hopefully this will help someone in the future.

Make sure app/assets/javascripts/application.js contains:

 //= require jquery //= require jquery_ujs 

And this erb contains <%= javascript_include_tag "jquery", "jquery_ujs" %> , and not just the <script> .

This is what fixed it for me.

0
source

All Articles