How to update a ruby variable from within the .erb view file?

I have a very simple view that displays "tasks".

After the user enters input, I'd basically like to update the task value. However, I can't execute the ruby statement to update the task in my .erb.

I'm not sure how I can update the value in my .rb file once the input is entered. Any help would be appreciated.


The Ruby code runs before the page is served to the user, the Javascript code runs in the user client while he is on the page, therefore you can't exactly run a Ruby code from the Javascript. What you can do, is use Javascript to send a asynchronous requisition to your ruby server, that will get the variables and do the save you want to do.

Since you are using jQuery, take a look at it's post() and ajax() function. ( )

You can do something like

$.post("/my/handler", { name: "John" }, function(data) {
/* Do the updates on the page according to the data received from the server */ 
} );

Then you just need to set your rails routes do deal with POST requisitions to /my/handler. ( may be useful ).

The webpage you will render will be the data that will be used on the function(data) on the callback of $.post, it don't need to be a 'normal' webpage, you can render any type of textual data, like json.

Take a look at , section "rendering json", since it's probably the most useful option to render the data you'll need on the js.

But keep in mind that, if it's an application that the user will be hitting the keyboard a lot and you need to process it all fast, you are going to make one requisition to your server every time he does that (which is like opening a page each time, only lighter because you will return just a small piece of text instead of the whole webpage data, but if it's a lot of requisitions, it can add a lot to your server load). If that's the case and there will be a lot of simultaneous users, you may consider building your application using sockets for a persistent connection.

Need Your Help

Developing app, wont run in iOS native FB app

facebook ssl canvas https proxy

I'm in the process of developing an FB app, which needs to be able to run in the usual desktop FB site (with surrounding FB livery), on mobile device browsers (I'm using HTML5 and Bootstrap to adap...