eldarion / eldarion-ajax

a library for adding declarative ajax functionality to your website
BSD 3-Clause "New" or "Revised" License
758 stars 153 forks source link

Compatibility with jquery_ujs in Rails? #47

Closed r4m closed 11 years ago

r4m commented 11 years ago

Hi, I'm trying to use your js, which I find awesome, in a Rails app. Get requests are ok but I'm stucked into post/put requests. The problem is that for each post/put there are two requests that are routed to the backend, the first one is successfully completed, the second one fails:

Started POST "/todos/undone/6" for 127.0.0.1 at 2013-07-13 09:23:57 +0200
Processing by TodosController#mark_undone as JSON
  [...]
  Rendered todos/_todo.html.erb (2.5ms)
Completed 200 OK in 52ms (Views: 0.8ms | ActiveRecord: 23.8ms)

Started POST "/todos/undone/6" for 127.0.0.1 at 2013-07-13 09:23:57 +0200
Processing by TodosController#mark_undone as HTML
  [...]
  Rendered todos/_todo.html.erb (1.9ms)
Completed 406 Not Acceptable in 15ms (ActiveRecord: 3.0ms)

At the first attempt everything is accomplished, giving back a json with the html content that the frontend is waiting for. The second one fails because the HTML response is not rendered by the backend (which is correct, since I've to send back a json to the eldarion-ajax success).

I do not know if this is happing due a conflict between jquery_ujs, which include all the functionalities of rails.js for the standard rails ajax requests, with eldarion-ajax.

Someone has an idea regarding that? Thanks!

Notice: if I disable jquery_ujs the backend response is the following:

WARNING: Can't verify CSRF token authenticity
  [...]
Completed 401 Unauthorized in 10ms
paltman commented 11 years ago

@r4m Sorry, I am not that familiar with Rails. This doesn't seem to be a front end issue but something your controller doesn't like about the request data perhaps. What are the reasons your controller would return a 406 Not Acceptable?

paltman commented 11 years ago

@r4m could this be it? http://stackoverflow.com/questions/3751030/rails-3-returning-a-http-406-not-acceptable

paltman commented 11 years ago

@r4m also http://stackoverflow.com/questions/7808051/rails-completed-406-not-acceptable

r4m commented 11 years ago

I just solved it in this way:

1) remove jquery_ujs from application.js, in this way when an ajax call is started only 'eldarion-ajax' processes it

2) add javascript beforeSend: function(xhr) {xhr.setRequestHeader('X-CSRF-Token', $('meta[name="csrf-token"]').attr('content'))}, to eldarion-ajax-core.js.

I'm gonna fork your project to add this and eventually push it to your repo if it could be useful for other guys

r4m commented 11 years ago

I'll move further to understand if it is possible to maintain compatibility between eldarion-ajax.js and rails.js just in case I need it. Thanks for your instant-support @paltman ;)

paltman commented 11 years ago

@r4m I think that line with adding the CSRF token should remain outside of the eldarion-ajax library as I believe different frameworks and/or site developers will use CSRF tokens differently. For instance, I include this file in all my Django projects and it takes care of AJAX CSRF but is a bit different than your solution:

https://github.com/pinax/pinax-theme-bootstrap/blob/master/pinax_theme_bootstrap/static/pinax/js/theme.js#L17

r4m commented 11 years ago

@paltman I agree infact I've seen the link you have referred through my chrome-web-dev noticing that you have managed by yourself the CSRF ;) from that I was able to run your lib on RoR. Thanks