javascript - Rails jquery sortable not saving position correctly -


i have sortable table , seems work fine, order off when refresh page. correct , in right spot except last row moved, in reverts 1 row beneath row should occupying. (example: if wanted order 1,4,2,3 , last box moved place box 4; on refresh 1,2,4,3) makes sense.

my jquery:

jquery ->   if $('#sortable').length > 0     table_width = $('#sortable').width()     cells = $('.table').find('tr')[0].cells.length     desired_width = table_width / cells + 'px'     $('.table td').css('width', desired_width)      $('#sortable').sortable(       axis: 'y'       items: '.item'       cursor: 'move'        sort: (e, ui) ->         ui.item.addclass('active-item-shadow')       stop: (e, ui) ->         ui.item.removeclass('active-item-shadow')       update: (e, ui) ->         item_id = ui.item.data('item-id')         console.log(item_id)         position = ui.item.index() # not work paginated items, index 0 on every page         $.ajax(           type: 'post'           url: '/stripboards/update_row_order'           datatype: 'json'           data: { stripboard: {stripboard_id: item_id, row_order_position: position } }         )     ) 

my controller:

class stripboardscontroller < applicationcontroller     def index         @stripboards = stripboard.rank(:row_order).all     end      def new         @stripboard = stripboard.new     end      def edit         @stripboard = stripboard.find(params[:id])     end      def create         @stripboard = stripboard.new(stripboard_params)           if @stripboard.save             redirect_to stripboards_path         else             render 'new'         end     end      def destroy         @stripboard = stripboard.find(params[:id])         @stripboard.destroy          redirect_to stripboards_path     end      def update_row_order         @stripboard = stripboard.find(stripboard_params[:stripboard_id])         @stripboard.row_order_position = stripboard_params[:row_order_position]         @stripboard.save          render nostripboard: true # post action, updates sent via ajax, no view rendered     end      private      # use callbacks share common setup or constraints between actions.     def set_stripboard         @stripboard = stripboard.find(params[:id])     end      # never trust parameters scary internet, allow white list through.     def stripboard_params         params.require(:stripboard).permit(:stripboard_id, :title, :scene, :scene_type, :location, :description, :time, :pages, :characters, :production_id, :row_order_position)     end end 

my html:

<table class="table table-bordered table-striped" id="sortable">   <tr>     <th class="col-sm-1">scene #:</th>     <th class="col-sm-1">int/ext:</th>     <th class="col-sm-1">time:</th>     <th class="col-sm-4">set:</th>     <th class="col-sm-2">location:</th>     <th class="col-sm-1">pages:</th>     <th class="col-sm-2">characters</th>     <th></th>   </tr>   <% @stripboards.each |stripboard| %>     <tr data-item-id=<%= "#{stripboard.id}" %> class="item" style="       <% if stripboard.time == 2 && stripboard.scene_type == 1 %>background-color:#ffffff;<% end %>       <% if stripboard.time == 2 && stripboard.scene_type == 2 %>background-color:#ffff00;<% end %>       <% if stripboard.time == 4 && stripboard.scene_type == 1 %>background-color:#00008b;color:#fff;<% end %>       <% if stripboard.time == 4 && stripboard.scene_type == 2 %>background-color:#006400;color:#fff;<% end %>       <% if stripboard.time == 1 %>background-color:#ffc0cb;<% end %>       <% if stripboard.time == 3 %>background-color:#f4a460;<% end %>       ">       <td class="col-sm-1"><%= stripboard.scene %></td>       <td class="col-sm-1">         <% if stripboard.scene_type == 1 %>int<% end %>         <% if stripboard.scene_type == 2 %>ext<% end %>       </td>       <td class="col-sm-1">         <% if stripboard.time == 1 %>morning<% end %>         <% if stripboard.time == 2 %>day<% end %>         <% if stripboard.time == 3 %>evening<% end %>         <% if stripboard.time == 4 %>night<% end %>       </td>       <td class="col-sm-4"><%= stripboard.title %><br /><%= stripboard.description %></td>       <td class="col-sm-2"><%= stripboard.location %></td>       <td class="col-sm-1"><%= stripboard.pages %></td>       <td class="col-sm-1"><%= stripboard.characters %></td>       <td class="col-sm-2"><%= link_to 'delete', stripboard_path(stripboard), class: "btn btn-danger btn-sm",               method: :delete,               data: { confirm: 'confirm want permanently delete strip.  action cannot undone, , delete data associated strip.' }  %></td>     </tr>   <% end %> </table> 

my db:

create_table "stripboards", force: true |t|     t.string   "title"     t.string   "scene"     t.integer  "scene_type",    limit: 255     t.string   "location"     t.string   "description"     t.integer  "time",          limit: 255     t.string   "pages"     t.string   "characters"     t.datetime "created_at"     t.datetime "updated_at"     t.string   "production_id"     t.integer  "row_order"     t.integer  "stripboard_id"   end 

i seem recognize code http://benw.me/posts/sortable-bootstrap-tables/. got working myself :)

in update_row_order function you've renamed, i'm guessing accidentally, nothing symbol nostripboard. bit of find-and-replace on "thing" gone wild, perhaps?

try updating last line function reads so:

def update_row_order   @stripboard = stripboard.find(stripboard_params[:stripboard_id])   @stripboard.row_order_position = stripboard_params[:row_order_position]   @stripboard.save    render nothing: true # post action, updates sent via ajax, no view rendered end 

the render nothing: true part tells rails render nothing - use no views, no templates, respond http 200 ok status.

since see no other problems code, try , report back. i'd advise using network tab of dev console/dev tools in favorite browser (i know firefox , chrome have one, haven't tried in others). allow sniff traffic send ajax , mark, example, 500 status responses. should read development server's logs , errors in there - i'm sure there'll error if in there.


Comments