我尝试在Rails 4的应用程序中使用Ajax。要将我的js发送到我使用的客户端:
respond_to do |format|
format.js
end
在我的控制器中。 但是我的控制器产生了错误“ActionController :: UnknownFormat”。 有人可以帮帮我吗?
main_controller.rb:
class Oweb::MainController < ApplicationController
def index
.....
end
def setmagasinstatus
begin
@mag = Magasin.find(params[:id])
if @mag.etatmagasin.lib == 'Ouvert'
@mag.etatmagasin = Etatmagasin.where(lib: 'Fermé').first
else
@mag.etatmagasin = Etatmagasin.where(lib: 'Ouvert').first
end
rescue ActiveRecord::RecordNotFound
logger.error("Attempt to access invalid Magasin #{params[:id]}")
redirect_to_index("Invalid Magasin")
else
@mag.save
@mags = Magasin.where(user_id: current_user.id)
respond_to do |format|
format.js
end
end
end
end
setmagasinstatus.js.erb:
page.replace_html("bloc_magasin", :partial => "listmagasins", :object => @mags)
布局/ application.html.slim:
doctype html
html
head
meta charset="utf-8"
meta http-equiv="X-UA-Compatible" content="IE=edge"
meta name="viewport" content="width=device-width, initial-scale=1.0"
meta name="description" content=""
meta name="author" content=""
title = yield(:title)
= stylesheet_link_tag "application"
= csrf_meta_tags
body
.........
= yield
= javascript_include_tag "application"
= yield :scripts
的Gemfile:
source 'https://rubygems.org'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.1.1'
group :development do
gem 'mysql2'
end
group :production do
gem 'pg'
end
gem 'sass-rails', '~> 4.0.3'
gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.0.0'
gem 'therubyracer', platforms: :ruby
gem 'jquery-rails'
gem 'turbolinks'
gem 'jbuilder', '~> 2.0'
gem 'sdoc', '~> 0.4.0', group: :doc
gem 'spring', group: :development
gem 'devise', '~> 3.2.4'
gem 'slim-rails', '~> 2.1.4'
gem 'bootstrap-sass', '~> 3.1.1.1'
gem 'compass-rails', '~> 1.1.7'
gem 'simple_form', '~> 3.0.2'
gem 'activeadmin', github: 'gregbell/active_admin'
gem 'polyamorous', github: 'activerecord-hackery/polyamorous'
gem 'ransack', github: 'activerecord-hackery/ransack'
gem 'formtastic', github: 'justinfrench/formtastic'
gem 'debugger', group: [:development, :test, :production]
gem 'geocoder'
gem 'gmaps4rails', '~> 2.1.2'
日志:
Started GET "/oweb/main/setmagasinstatus?id=3" for 192.168.56.1 at 2014-08-01 06:22:06 +0000
Processing by Oweb::MainController#setmagasinstatus as HTML
Parameters: {"id"=>"3"}
Completed 406 Not Acceptable in 103ms
ActionController::UnknownFormat (ActionController::UnknownFormat):
app/controllers/oweb/main_controller.rb:24:in `setmagasinstatus'
Rendered /home/vagrant/.rvm/gems/ruby-2.1.1/gems/actionpack-4.1.1/lib/action_dispatch/middleware/templates/rescues/_source.erb (2.1ms)
Rendered /home/vagrant/.rvm/gems/ruby-2.1.1/gems/actionpack-4.1.1/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (2.2ms)
Rendered /home/vagrant/.rvm/gems/ruby-2.1.1/gems/actionpack-4.1.1/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (1.1ms)
Rendered /home/vagrant/.rvm/gems/ruby-2.1.1/gems/actionpack-4.1.1/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (27.1ms)
答案 0 :(得分:12)
<强>捕捉:强>
您的控制台日志显示:
Oweb :: MainController处理#setmagasinstatus 为HTML
在您的控制器操作中,您不支持HTML响应。这就是您遇到UnknownFormat
错误的原因。
<强>解决方案:强>
在您的ajax调用中添加format: "js"
。例如:
$.ajax url: "/oweb/main/setmagasinstatus", data: 'id=' + id, format: 'js'
答案 1 :(得分:11)
如果您可以使用link_to
辅助函数来调用控制器的操作,那么Oweb::MainController#setmagasinstatus
将:remote => true
添加到link_to
帮助程序将解决问题。
例如,如果您有一个指向您的操作的链接并希望在ajax中获得响应,则以下代码将创建指向该操作的链接:
<%= link_to your_link_name, setmagasinstatus_path(:params), :remote => true %>
这是一个简单的样本https://coderwall.com/p/kqb3xq/rails-4-how-to-partials-ajax-dead-easy
答案 2 :(得分:6)
<强> HTML 强>
看起来问题是您因任何原因发送HTML
请求:
Oweb :: MainController#setmagasinstatus as HTML
我猜测&#34;未知格式&#34;因此,您的操作中缺少html
mime-type的定义会导致错误。我想这会解决表面层面的错误:
respond_to do |format|
format.js
format.html
end
<强>的Ajax 强>
如果上述内容正确,则问题转为&#34;为什么要发送HTML mime-type?&#34;
由于无法看到您的实际Ajax代码,我建议问题 或者只是发送标准HTTP请求(ajax是{{ 3}});或者您对使用ajax请求的声明有疑问
您必须记住,Ajax(XHR)是代表您发送请求的 - 就像伪浏览器一样。这意味着如果你想发送一个ajax请求,它必须通过Javascript完成(因此format.js
mime类型处理程序)
-
就我个人而言,我认为您并未发送ajax请求,但也可能是您发送了不同的dataType
,例如JSON