多形モデルComment
、Post
または別のComment
(redditスタイル)のコメントがあります。私は、ユーザーが@post.id
のcommentable_id
でポストに直接コメントできるようにしたいと@post.class.name
Rails 5:1つのビューで1つの多型モデルの2つのフォーム
OR
のcommentable_type
は@comment.id
を使用して、しかし、同じページ上の別のコメントにコメントすることができ、および@comment.class.name
フォームがどこにあるかによって、@post
または@comment
の間で動的に変更する方法はありますか?
ここに私が役立つと思われる関連情報があります。
私のフォーム部分comment_list
インサイド
# 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>
そして、私のビュー
# 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 %>
が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>
EDITの別の部分になります
:
私のルートも追加しています。
# 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'を表示できますか? –