2012-11-25 2 views
0

ryanb에 의해 설명 된 방식으로 주석 시스템을 시도하고 있습니다 (https://github.com/railscasts/154-polymorphic-association/tree/master/revised/blog-after).Rails 3가 중첩 된 다형성 리소스로 연결

그것은 레일을 routes.rb에서 볼 3 중첩 된 리소스 라우팅을 사용

resources :articles do 
    resource :comments 
end 
articles_controller.rbcomments_controller.rb에서 볼 수 있듯이

댓글은 부모 유형과 ID로로드 :

class ArticlesController < ApplicationController 
    ... 
    def show 
    @article = Article.find(params[:id]) 
    @commentable = @article 
    @comments = @commentable.comments 
    @comment = Comment.new 
    end 

class CommentsController < ApplicationController 
    before_filter :load_commentable 

    def index 
    @comments = @commentable.comments 
    end 
    ... 
private 

    def load_commentable 
    resource, id = request.path.split('/')[1, 2] 
    @commentable = resource.singularize.classify.constantize.find(id) 
    end 
    ... 
end 

어떻게 추가 가겠어요 댓글에 대한 링크 편집 또는 댓글보기 템플릿의 액션 삭제 배열로

_comments.html.erb:

<% @comments.each do |comment| %> 
    <div class="comment"> 
    <%= simple_format comment.content %> 
    <%= link_to "Delete", comment, method: :delete %> 
    </div> 
<% end %> 

답변

0

나는 통과 배열에 더 많은 모습과 routes.rb 자원에서의 누락 된주의 : 의견. 고정 버전 :

resources :articles do 
    resources :comments do 
    end 
    end 

이제 템플릿은 링크를 배열로 제공하여 완벽하게 작동합니다.

<% @comments.each do |comment| %> 
    <div class="comment"> 
    <%= simple_format comment.content %> 
    <%= link_to "Delete", [@commentable, comment], method: :delete %> 
    </div> 
<% end %> 
1

패스 자원 : jdoes 조언으로

<%= link_to "Delete", [@article, @comment], method: :delete %> 
관련 문제