2016-11-05 13 views
0

多形モデルCommentPostまたは別のComment(redditスタイル)のコメントがあります。私は、ユーザーが@post.idcommentable_idでポストに直接コメントできるようにしたいと@post.class.nameRails 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 

答えて

0

は、あなただけの部分的にローカルとして、コメントや記事を渡すことができませんでしたか?

コメントにコメントの場合:投稿にコメントするために

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

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

そしてshared/_comment_formに:

<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'を表示できますか? –

関連する問題