Rails 4 + -jquery-datatable + ajax-datatable - Http and Actioncontroller error 406:Unknownformat format json

Asked

Viewed 113 times

0

I was studying Rails and following the tutorial https://github.com/antillas21/ajax-datatables-rails.

The tutorial steps were the following, create a datatable that would perform a request via json, the controller that would respond with the data and the data table would be populated.

At the time of testing I received the following server error: 406 Not Acceptable, and detailing the error by Chrome in the network tab, the error below is displayed:

Actioncontroller::Unknownformat at /Categories.json%20

ActionController::UnknownFormat

app/controllers/categories_controller.Rb, line 8

``` ruby 3 before_action :set_category, only: [:show, :Edit, :update, :Destroy] 4
5 # GET /Categories 6 # GET /Categories.json 7 def index

8 respond_to do |format| 9 html format. 10 format.json { render json: Categorydatatable.new(view_context)} 11 end 12 end 13
```

I researched but could not reach or find a solution.

Follow step by step the code in case anyone can help me I’ll be grateful:

View:

index.html.erb

Coffe: Responsible for updating the datatable when called by the view categories.js.coffe

Controller - index method: categories_controller.rb

Browser error : Network tab: inserir a descrição da imagem aqui

Detailed browser error: inserir a descrição da imagem aqui

In other words, the server I understood does not accept the call via json, but I couldn’t figure out how to make it accept. The tutorials or similar errors I found are the same and according to the authors work.

If I make the request directly in the browser http://localhost:3000/Categories.json the file is displayed smoothly: {"draw":0,"recordsTotal":3,"recordsFiltered":3,"date":[[1,"Teste1","Error","Yes"],[2,"Teste2","Improvement","Yes"],[4,"Teste3","User Error","No"]]}

If anyone can help me I’d be grateful.

1 answer

1


The error is indicating that Rails does not recognize the format used in the request.

In the view the value of the attribute data-source has a space after the url, so the request made by Datatable is http://localhost:3000/Categories.jsonspace and Rails is trying to render the . json formatspace

Just remove the space at the end of the data-source.

  • Vlw friend thank you very much...I do not believe in this... a space... and thinking that the mistake was something otherworldly...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.