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.rb 및
comments_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.each do |comment| %>
<div class="comment">
<%= simple_format comment.content %>
<%= link_to "Delete", comment, method: :delete %>
</div>
<% end %>