2014-06-16 3 views
0

jQuery를 사용하여 내 테이블을 정렬 가능하게 (드래그 앤 드롭)하려고합니다. 문제는 새 정렬 된 테이블을 저장 한 다음 브라우저를 다른 사용자에게 게시하는 것입니다. 새로 고침.Rails jQuery sortable을 저장할 수 없습니다.

내가 사용 :

보석 'acts_as_list' 레일 3.2.17 루비를 1.9.3p484

는이 일을 더 좋은 방법이 있나요?

내 컨트롤러

데프 종류의 @episodes = current_user.episodes_for (@show) .each 할 내 종류와 표시 방법 | 에피소드 | episode.position = params를 [ '에피소드'] 지수 (episode.id.to_s) + 1 episode.save 끝 렌더링 :. 아무것도 => 진정한 끝

def show 
    @episodes = current_user.episodes_for(@show) 
    @episodes.order('episode.position ASC') 
    @episode = @episodes.where(id: params[:id]).first 
    end 

내 자바 스크립트

<script> 
$(window).load(function() { 
    $("#sortthis").sortable({ 
     axis: 'y', 
     placeholder: 'ui-state-highlight', 
     cursor: 'move', 
     dropOnempty: false, 
     scroll: true, 
     opacity: 0.4, 

     update: function(event, ui){ 
     var itm_arr = $("#sortthis").sortable('toArray'); 
     var pobj = {episodes: itm_arr}; 
     $.post("/episodes/sort", pobj); 
     } 

    }); 
}); 
</script> 
내가보기에 일종의 싶어

표 :

<tbody id='eps'> 
<tr> 
    <th>Title</th> 
    <th>#</th> 
    <th>Season</th> 
    <th>Download?</th> 
    <th>Shared?</th> 
    <th>Length</th> 
    <th>Status</th> 
     <th></th> 
</tr> 
<% for episode in @episodes %> 
    <tr> 
    <td id="eps_<%= episode.id %>", width="20%"><%=h episode.title %></td> 
    <td id="eps_<%= episode.id %>"><%=h episode.number %></td> 
    <td id="eps_<%= episode.id %>"><%=h episode.season %></td> 
    <td id="eps_<%= episode.id %>"><%=h episode.is_downloadable %></td> 
    <td id="eps_<%= episode.id %>"><%=h episode.shared_with_dev %></td> 
    <td id="eps_<%= episode.id %>"><%=h episode.length %></td> 
    <td> 
     <% if episode.video %> 
     <%= link_to 'Replace Video', new_show_episode_video_path(episode.show, episode) %> 
     <% else %> 
     <%= link_to 'Upload Video', new_show_episode_video_path(episode.show, episode) %> 
     <% end %> 
    </td> 
     <td> 
     <%= link_to "View", [episode.show, episode] %> &nbsp; 
    <%= link_to "Edit", edit_show_episode_path(episode.show, episode) if permitted_to? :update, episode %> &nbsp; 
    <%= link_to "Delete", show_episode_path(episode.show, episode), :confirm => 'Are you sure?', :method => :delete if permitted_to? :delete, episode %> 
    </td> 
    </tr> 
<% end %> 

어떤 도움을 주시면 감사하겠습니다!

답변

0

나는 그것을 알아 낸 다음과 같이 변경했다 :

조회수 :

<tr id="<%= episode.id %>"> 

def sort 
    Episode.all.each do |e| 
    if position = params[:episodes].index(e.id.to_s) 
     e.update_attribute(:position, position + 1) unless e.position == position + 1 
    end 
    end 
    render :nothing => true, :status => 200 
end 

마침내 내 모델이 추가

새로운 컨트롤러 :

acts_as_list 
    default_scope order('position') 

비슷한 문제가있는 사람이 있는지 확인하십시오.