다형성 모델 Comment
, Post
또는 다른 Comment
(reddit 스타일)에 대한 주석이 있습니다. 나는 사용자가 @post.id
의 commentable_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
더 가까워집니다. 나는 네가 그런 사람들을 지나칠 수 있다는 것을 몰랐다. 지역의 이름에 따라'form_for' 행을 전달할 것인지 결정하기 위해 erb'if' 블록을 추가 할 수 있습니까? 그 모양을 깨뜨릴 까 아니면 내가 완전히 여기에서 벗어나겠습니까? – Antonio
또한 원래 게시물을 관련 경로로 업데이트했습니다. – Antonio
문제는 경로에서 게시물 아래에 댓글이 중첩되어 있지만 모든 댓글이 게시물과 관련되어 있다고 가정하기 때문에 댓글 설명의 경우는 그렇지 않습니다. 'PostsController'를 보여줄 수 있습니까? –