2015-02-03 6 views
0

이 항목에 대한 많은 질문이 있지만 작동하는 것으로는 보이지 않습니다. 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 ('경로 일치 없음'오류를 제거하기 위해) 또는 첫 번째 경로의 올바른 경로 일지에 대해 확신 할 수 없다는 것입니다. 당신은 일반적으로 하나의() 호출을 원하는대로 당신이 그것을 도울 수 있다면 코드에서

답변

1

우선은,

@ordered = @post.questions.where(:hide => true, :recommend => true).order("position") 

에 선

@ordered = @post.questions.where(:hide => true).where(:recommend => true).order("position") 

을 변경합니다. 때로는 조건부로 하나를 추가해야합니다. 예를 들어 if 블록에서.

경로 오류가 발생하면 터미널에 rake routes을 실행하면 모든 경로 방법, 허용되는 매개 변수, HTTP 메서드 및 해당 컨트롤러가 부딪히는 동작이 표시됩니다.

중첩 된 리소스에 대해주의해야 할 중요한 점은 중첩 된 리소스가 부모의 "멤버"에 적용된다는 것입니다.귀하의 경우 그래서 당신이 개 자원 블록을 생성하는 것은 다음과 같습니다

<ul id="questions" data-update-url="<%= sort_comment_questions_path(@comment) %>"> 

문제는 생각입니다 :

데이터 속성에 ERB 태그에 따라서
GET /comments/:comment_id/questions questions#index 
GET /comments/:comment_id/questions/:id questions#show 
POST /comments/:comment_id/questions/sort questions#sort 

, 당신은 여기에 주석을 추가 할 필요가 게시물 모델 수준에서 이것을 사용하고 있으며 많은 의견이 있습니다. 그래서 당신은 아마 원하는 것은 이것이다 : 당신의보기에서 다음

resources :comments do 
    resources :questions 
end 

resources :posts do 
    member do 
    post "sort" => "questions#sort", :as => "sort_questions" 
    end 
end 

:

<ul id="questions" data-update-url="<%= sort_questions_post_path(@post) %>"> 
+0

대, 기본적으로 고정하는. (그냥 sort_post_questions_path를 sort_post_path로 변경해야했습니다). 동료 젊은 캐나다 사람은 도움을 평가한다 :) – user2759575

+0

아 좋아. 그게 내가 가진 것처럼 작동하게 만들었어야 했어. 하지만 꼭 필요한 것은 아닙니다 :) – agmcleod

관련 문제