简体   繁体   中英

respond_to do |format| format.js not working

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')%>")   

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}"

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