gpt4 book ai didi

ruby-on-rails - ActionController::UnknownFormat 和 format.js 用于 ajax 实现(Rail 4)

转载 作者:行者123 更新时间:2023-12-03 21:16:54 26 4
gpt4 key购买 nike

我尝试在 Rails 4 的应用程序中使用 Ajax。要将我的 js 发送到客户端,我使用:

   respond_to do |format|
format.js
end

在我的 Controller 中。
但是它在我的 Controller 中生成了错误“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

gem 文件:
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)

最佳答案

捕获:

您的控制台日志说:

Processing by Oweb::MainController#setmagasinstatus as HTML



在您的 Controller 操作中,您没有受支持的 HTML 响应。这就是为什么您面临 UnknownFormat 错误的原因.

解决方案:

添加 format: "js"在你的 ajax 调用中。例如:
$.ajax url: "/oweb/main/setmagasinstatus", data: 'id=' + id, format: 'js'

关于ruby-on-rails - ActionController::UnknownFormat 和 format.js 用于 ajax 实现(Rail 4),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25074739/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com