respond_to do | format | format.js无法正常工作

时间:2015-01-06 02:57:54

标签: ruby-on-rails ruby actioncontroller respond-to

我需要启用/安装以使我的respond_to块返回js?

Rails 4.2.0

ruby​​ 2.2(也尝试使用4.0 ...我降级为匹配设置为工作......) 控制台返回错误:

CameraController处理#显示为HTML 已完成406在2ms内无法接受

ActionController :: UnknownFormat(ActionController :: UnknownFormat):

http://apidock.com/rails/Mime提到js是Rails的DEFAULT mime类型。我尝试将它添加到头文件但是在控制台中返回了一条消息,说我不需要将它包含在头文件中。我错过了什么?

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

1 个答案:

答案 0 :(得分:1)

而不是

=link_to "Camera", camera_show_path, remote: true

尝试使用

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

如果这不起作用,请在相机/ show.js.erb中添加

window.location="#{cameras_path}"