i try to use Ajax in my application with Rails 4. To send my js to the client i use :
respond_to do |format|
format.js
end
in my controller. But it's generated error "ActionController::UnknownFormat" i my controller. Someone can help me please ?
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'
logs:
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)
Catch:
Your console log says:
Processing by Oweb::MainController#setmagasinstatus as HTML
In your controller action, you don't have supported HTML response. That's why you are facing error of UnknownFormat
.
Solution:
Add format: "js"
in your ajax call. For example:
$.ajax url: "/oweb/main/setmagasinstatus", data: 'id=' + id, format: 'js'
If you can use link_to
helper function to call action of your controller, in your case Oweb::MainController#setmagasinstatus
adding :remote => true
to link_to
helper will solve the issue.
For example if you have a link to your action and want to get response in ajax the following code will create the link to that action:
<%= link_to your_link_name, setmagasinstatus_path(:params), :remote => true %>
Here is a dead easy sample https://coderwall.com/p/kqb3xq/rails-4-how-to-partials-ajax-dead-easy
HTML
Looks like the problem is you're sending an HTML
request for whatever reason:
Oweb::MainController#setmagasinstatus as HTML
I'd surmise the "unkown format" error will therefore be caused by your lack of definition for the html
mime-type in your action. I'd imagine this would work to resolve the error at surface-level:
respond_to do |format|
format.js
format.html
end
Ajax
If the above is correct, the question then turns to "why is an HTML mime-type being sent?"
Due to not being able to see your actual Ajax code, I'd recommend the problem will either be that you're just sending a standard HTTP request (ajax is XHR); or you'll have an issue with the declaration with the ajax request
You must remember that Ajax (Asynchronous Javascript And XML) is meant to send a request on your behalf -- like a pseudo browser. This means if you want to send an ajax request, it has to be done through Javascript (hence the format.js
mime type handler)
--
Personally, I think you're not sending an ajax request, although it could also be that you're sending a different dataType
, such as JSON
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With