Rails HTML Table update fields in mongo with AJAX

Posted by qwexar on Stack Overflow See other posts from Stack Overflow or by qwexar
Published on 2012-11-24T16:54:41Z Indexed on 2012/11/24 17:03 UTC
Read the original article Hit count: 153

Filed under:
|
|
|

I'm building a Rails app backed by mongodb using mongoid.

It's a one page app, with a HTML table, every field for every row of which, needs to be editable without refreshing the page.

This is your usual Rails view ( like many in rails casts ) showing a table with rows and columns containing data.

For example. I'm showing cars, and showing their make, model and notes

They way I'm doing this is by appending _id of a mongo document to every column and marking it's field name in html id too. Then I pick up the value for $("#id") and send it to rails controller via AJAX and run @car.update_attributes method accordingly.

Currently, one of my rows looks like this.

    <tr>
       <td id=<%= car.id %>_make>
       <%= car.make %>
       </td>

       <td id=<%= car.id %>_model>
       <%= car.model %> 
       </td>

       <td id=<%= car.id %>_notes>
       <%= car.notes %>
       </td>
     </tr>

     // my function which is called onChange for every column
     function update_attributes(id){
        var id = id.split[0];
        var attribute = id.split[1];
        $.ajax("sending id and attribute to rails controller");
     }

Is there any built it Rails magic which would let me update only a field in a model without refreshing the page?

or.

Is there a Rails plugin for this?

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about AJAX