一尘不染

带有format.js的ActionController :: UnknownFormat用于Ajax实现(第4条)

ajax

我尝试在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)

layouts / 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)

阅读 254

收藏
2020-07-26

共1个答案

一尘不染

抓住:

您的控制台日志显示:

由Oweb :: MainController#setmagasinstatus处理 为HTML

在您的控制器操作中,您不支持HTML响应。因此,您将面临的错误UnknownFormat

解:

添加format: "js"您的ajax调用。例如:

$.ajax url: "/oweb/main/setmagasinstatus", data: 'id=' + id, format: 'js'
2020-07-26