이 항목에 대한 많은 질문이 있지만 작동하는 것으로는 보이지 않습니다. this railscast를 사용하여 jquery-ui를 사용하여 질문 목록을 정렬하려하지만 this과 같은 질문은 내 중첩 된 리소스가 혼란 스럽습니다.jquery를 사용하여 중첩 된 리소스로 질문 정렬
게시물, 댓글 및 질문의 세 가지 모델이 있습니다.
Post.rb :
class Post < ActiveRecord::Base
has_many :comments
has_many :questions, :through :comments
end
Comment.rb
class Comment < ActiveRecord::Base
belongs_to :post
has_many :questions
end
Question.rb
class Question < ActiveRecord::Base
belongs_to :comment
end
I 정렬하고 싶은 질문의 목록은 ordered_path보기에 (posts/: id/ordered).
Posts_controller.rb
def ordered
@post = Post.friendly.find(params[:id])
@ordered = @post.questions.where(:hide => true).where(:recommend => true).order("position")
end
및 questions_controller.rb :
def sort
params[:question].each_with_index do |id, index|
Question.update_all({position: index+1}, {id: id})
end
render nothing: true
end
내가 제대로 railscast을 따랐습니다 믿고 여기에 게시물 컨트롤러입니다. 질문에 '위치'항목을 추가했습니다.
routes.rb
resources :comments do
resources :questions do
collection { post :sort }
end
end
그리고 내보기에 나는이
게시물이/ordered.html.erb
<ul id="questions" data-update-url="<%= sort_comment_questions_path %>">
<% @ordered.each do |question| %>
<%= content_tag_for :li, question do %>
<span class="handle">[drag]</span>
<%= question.body %>
<% end %>
<% end %>
</ul>
마지막 게시물 : 나는 노선이 추가되었습니다. js.coffee :
jQuery ->
$('#questions').sortable
axis: 'y'
handle: '.handle'
update: ->
$.post($(this).data('update-url'), $(this).sortable('serialize'))
문제점 (들)은 data-update-url ('경로 일치 없음'오류를 제거하기 위해) 또는 첫 번째 경로의 올바른 경로 일지에 대해 확신 할 수 없다는 것입니다. 당신은 일반적으로 하나의() 호출을 원하는대로 당신이 그것을 도울 수 있다면 코드에서
대, 기본적으로 고정하는. (그냥 sort_post_questions_path를 sort_post_path로 변경해야했습니다). 동료 젊은 캐나다 사람은 도움을 평가한다 :) – user2759575
아 좋아. 그게 내가 가진 것처럼 작동하게 만들었어야 했어. 하지만 꼭 필요한 것은 아닙니다 :) – agmcleod