Ruby On Rails 3, form_tag remote on responding to Ajax request?

I posted this answer on Hacker News, but figured the Stack Overflow community might benefit as well :-).

I posted this answer on Hacker News, but figured the Stack Overflow community might benefit as well :-) In Rails 3, the javascript drivers are very hands-off (i.e. Unobtrusive). The problem you're having is that your app is returning to the browser a string of javascript, but there is nothing in the page that is then executing that javascript in the context of the page.

The rails. Js ujs driver binds to forms and links with data-remote=true, which is what the :remote => true is doing, to make them submit their requests remotely, but that is where the Rails magic stops. The good news is that the remote requests fires off some events you can bind to, which give you access to the data returned by the server (which fire off in the following order): ajax:before ajax:loading ajax:success ajax:complete ajax:failure ajax:after You need to bind an event to the ajax:success event of your form.So, if your form had the id "myform", you'd want something like this on your page: $('#myform').

Bind('ajax:success', function(evt, data, status, xhr){ eval(xhr. ResponseText); }); xhr. ResponseText is what your server returns, so this simply executes it as javascript.

Of course, it's proper to also bind to the failure event with some error handling as well. I usually don't even use the action.js. Erb method of returning javascript, I just have my controller render the HTML partial, and then I have a binding like this in the page: $('#myform').

Bind('ajax:success', function(evt, data, status, xhr){ $('#target-div'). Html(xhr. ResponseText); }); I'm actually in the middle of writing a full article on this, but hopefully this is enough to get you going.

EDIT: I finished that article, fully explaining remote links and forms in Rails 3. Maybe it will help: Rails 3 Remote Links and Forms: A Definitive Guide.

Thanks for the answer on HN and here. As an . Erb user with javascript I counted on $(document).

Ready(function ()... with sinatra and rails to kick off events – Mark Essel Oct 27 '10 at 22:05 Steve thanks for posting this, its helping me understand Ajax, and Rails alot. I posted an update, because I think the html rendered from rails does not seem to match up to what I would have expected. Anyone else getting this?

– kp212 Oct 27 '10 at 23:48 Lord, finally found my first issue, posted an update. – kp212 Oct 28 '10 at 2:03 Hmm, as I take another look at it, xhr. ResponseText is just a string, meaning putting that in your function wouldn't actually execute it.

Try changing "xhr. ResponseText" to "eval(xhr. ResponseText)".

I've updated my example with it. – jangosteve Oct 28 '10 at 6:59 Close to figuring this out. I have the firebug debugger showing I get the js back.

I will re-read your advice and see if I can understand correctly where my js scripts need to be placed. – kp212 Oct 28 '10 at 15:39.

Maybe that's causing problems and you should just a little more specific: $("#my_list"). Html(" "list.html. Erb"))%>"); I'm not sure if this helps, but are if you using in IE be aware that IE sends some headers that screw with how your controller responds.

So you may be sending an Ajax request with IE, but your Rails app thinks its just a plain html request. I've had to setup jQuery to first erase the current headers and then add just the javascript header: $. AjaxSetup({ 'beforeSend': function(xhr) {xhr.

SetRequestHeader("Accept",'');xhr. SetRequestHeader("Accept", "text/javascript")} }).

If you look at the rendered source of your page, you should notice an error in the fields attributes. The correct way to do it is as follows: 'list'}, :remote => true %> Notice the curly brackets, very important! Alternatively you could have done: true %.

Using list as your function name in the controller may be the problem. That name is used internally by Rails.

I posted a follow changing it from list to doit to make sure its not because of an internal method. Still not returning anything for the . Js rendering, I get the 406 error.

– kp212 Oct 27 '10 at 2:22.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions