4

What do I have to enable/install to get my respond_to block to return js?

Rails 4.2.0

ruby 2.2 (Also tried with 4.0... I downgraded to match setup as work...) The console returns error:

Processing by CameraController#show as HTML Completed 406 Not Acceptable in 2ms

ActionController::UnknownFormat (ActionController::UnknownFormat):

http://apidock.com/rails/Mime mentions that js is a DEFAULT mime type for Rails.. I tried adding it to the header file but that returned a message in the console saying that I did not need to include it in the header file... What am I missing?

#camera_controller.rb
class CameraController < ApplicationController
  # respond_to :js   #I have tried using this...
  def show
    respond_to do |format|
      format.js #{render 'show.js.erb'} #I have tried this too..
    end
  end
end

# 'home/sidebar.html.haml'
...
# =link_to "Menu Items", menu_items_index_path, :handlers => [:erb], :formats => [:js], remote:true
=link_to "Camera", camera_show_path, remote: true
...

# 'config/routes.rb'
...
get 'camera/show'
...


# camera/show.js.erb
$("#main_view").html("<%= escape_javascript(render :partial => 'camera/show')%>")   
Peter Black
  • 69
  • 1
  • 9

1 Answers1

1

Instead of

=link_to "Camera", camera_show_path, remote: true

try using

%a{href: "/camera/show.js", class: 'btn', 'data-remote' => true}

If that doesn't work, in your camera/show.js.erb add

window.location="#{cameras_path}"
Shifa Khan
  • 769
  • 6
  • 12
  • Thanks. I will try when I get home tonight. But I don't understand why I can't format it the way I have it written above. Seeing as how I do that every day at work. – Peter Black Jan 07 '15 at 20:17
  • Sorry. I worked late yesterday.. I tried both with no success. The %a{href... returned the correct pages, but the code was rendered as plain text. The second example returned the error: undefined local variable or method `window' for # – Peter Black Jan 10 '15 at 02:24
  • I updated my question to show my js.erb. It is only one line but I tried .replaceWith, .html, and .raw. They all returned the same result. (code rendered as plain text) – Peter Black Jan 10 '15 at 02:33