2016-11-05 2 views
0

다형성 모델 Comment, Post 또는 다른 Comment (reddit 스타일)에 대한 주석이 있습니다. 나는 사용자가 @post.idcommentable_id와 포스트에 직접 코멘트 할 수 있도록하려면하고 commentable_type@post.class.name레일 5 : 단일보기에서 하나의 다형성 모델에 대한 두 개의 형식

또는

@comment.id@comment.class.name를 사용하지만, 같은 페이지에 다른 의견에 대해 언급 할 수

양식이 어디에 위치하는지에 따라 @post 또는 @comment 사이에서 동적으로 변경하는 방법이 있습니까?

다음은 도움이 될 것으로 생각되는 관련 정보입니다. comment_list 부분 내부

# show.html.erb for a Post 
<p id="notice"><%= notice %></p> 
<h2>Post:</h2> 
<div class="well"> 
    <p> 
    <strong>Title:</strong> 
    <%= @post.title %> 
    </p> 
<% if @post.description %> 
    <p> 
    <strong>Description:</strong> 
    <%= @post.description %> 
    </p> 
<% end %> 
</div> 

<%= render "shared/comment_form" %> 

<% if @post.comments.any? %> 
    <%= render "shared/comment_list" %> # Inside of this partial will be another comment form for the a comment. 
<% else %> 
    <p>No comments yet</p> 
<% end %> 

이 다른 부분이 될 것입니다

# shared/_comment_form.html.erb 
<div id="comment-form" class="form-group"> 
    <%= form_for @comment, remote: true, url: post_comments_path(@post) do |f| %> 
    <%= f.text_area :body, required: true, rows: 5, class: "form-control" %> 
    <%= f.hidden_field :commentable_id, :value => @post.id %> 
    <%= f.hidden_field :commentable_type, :value => @post.class.name %> 
    <br> 
    <%= f.submit class: "btn btn-primary" %> 
    <% end %> 
</div> 
<span id="add-comment">Add a comment</span> 

그리고 내보기

내 양식 comment_form

# shared/_comment_list.html.erb 
<h2>Comments</h2> 
<ul> 
    <% @post.comments.each do |c| %> 
    <li><%= c.body %></li> 
     <%= render "shared/comment_form" %> # Here 
    <% end %> 
</ul> 

편집 :

나는 내 경로도 추가하고있다.

# Applicable routes 
resources :forums do 
    resources :posts,  shallow: true do 
    resources :comments, shallow: true 
    end 
end 

그리고 :

post_comments GET /posts/:post_id/comments(.:format)  comments#index 
        POST /posts/:post_id/comments(.:format)  comments#create 
new_post_comment GET /posts/:post_id/comments/new(.:format) comments#new 
     edit_comment GET /comments/:id/edit(.:format)   comments#edit 
      comment GET /comments/:id(.:format)    comments#show 
        PATCH /comments/:id(.:format)    comments#update 
        PUT /comments/:id(.:format)    comments#update 
        DELETE /comments/:id(.:format)    comments#destroy 
     forum_posts GET /forums/:forum_id/posts(.:format)  posts#index 
        POST /forums/:forum_id/posts(.:format)  posts#create 
     new_forum_post GET /forums/:forum_id/posts/new(.:format) posts#new 
      edit_post GET /posts/:id/edit(.:format)    posts#edit 
       post GET /posts/:id(.:format)     posts#show 
        PATCH /posts/:id(.:format)     posts#update 
        PUT /posts/:id(.:format)     posts#update 
        DELETE /posts/:id(.:format)     posts#destroy 
       forums GET /forums(.:format)      forums#index 
        POST /forums(.:format)      forums#create 
      new_forum GET /forums/new(.:format)     forums#new 
      edit_forum GET /forums/:id/edit(.:format)    forums#edit 
       forum GET /forums/:id(.:format)     forums#show 
        PATCH /forums/:id(.:format)     forums#update 
        PUT /forums/:id(.:format)     forums#update 
        DELETE /forums/:id(.:format)     forums#destroy 

답변

0

당신은 단지 부분에 로컬로 의견이나 게시물을 통과 할 수 없습니다?

<%= render "shared/comment_form", locals: { commentable: @comment } %> 

게시물에 코멘트 : shared/_comment_form에서 다음

<%= render "shared/comment_form", locals: { commentable: @post } %> 

그리고 : 코멘트에 코멘트

<div id="comment-form" class="form-group"> 
    <%= form_for @comment, remote: true, url: post_comments_path(@post) do |f| %> 
    <%= f.text_area :body, required: true, rows: 5, class: "form-control" %> 
    <%= f.hidden_field :commentable_id, :value => commentable.id %> 
    <%= f.hidden_field :commentable_type, :value => commentable.class.name %> 
    <br> 
    <%= f.submit class: "btn btn-primary" %> 
    <% end %> 
</div> 
<span id="add-comment">Add a comment</span> 

꽤 확실하지 않다 거기에있는 경로에 대해서는 post_comments_path(@post)이지만 의견에 대한 의견에는 무언가가 없을 것입니다. ct 관계가 게시물과 연결되므로 작동하지 않습니다. 라우팅에 따라이 값을 변경해야 할 수도 있습니다.

+0

더 가까워집니다. 나는 네가 그런 사람들을 지나칠 수 있다는 것을 몰랐다. 지역의 이름에 따라'form_for' 행을 전달할 것인지 결정하기 위해 erb'if' 블록을 추가 할 수 있습니까? 그 모양을 깨뜨릴 까 아니면 내가 완전히 여기에서 벗어나겠습니까? – Antonio

+0

또한 원래 게시물을 관련 경로로 업데이트했습니다. – Antonio

+0

문제는 경로에서 게시물 아래에 댓글이 중첩되어 있지만 모든 댓글이 게시물과 관련되어 있다고 가정하기 때문에 댓글 설명의 경우는 그렇지 않습니다. 'PostsController'를 보여줄 수 있습니까? –

관련 문제