2016-08-03 17 views
0

入れ子リソースを持つform_forに関する質問が1つあります。私はブログのようなものを作成し、投稿、コメント、コメントのコメント(返信など)を作成します。問題があります。そして、私はそれをコメントにしよう:コメントのform_forネストされたリソース(投稿、コメント)

new.html.erb:

<div class= "container" %> 
<%= form_for @comment do |f| %> 
    <%= f.input :title %> 
    <%= f.text_area :body %> 
    <%= f.submit %> 
<% end %> 
</div> 

マイ: "get_parentレンダリングや 完了302見つからをリダイレクトhttp://localhost:3000/ フィルター・チェーンにリダイレクトと停止"コメントコントローラ:

before_filter :get_parent 

    def new 
    @comment = @parent.comments.build 
    end 

    def create 
    @comment = @parent.comments.build(params[:comment]) 
    @comment.user_id = current_user.id 
    if @comment.save 
     redirect_to posts_path(@comment.post), :notice => 'Thank you for your comment!' 
    else 
     render :new 
    end 
    end 

    private 

    def comment_params 
    params.require(:comment).permit(:body, :title, :user_id, :commentable_id, :commentable_type) 
    end 


    def get_parent 
    @parent = Post.find_by_id(params[:post_id]) if params[:post_id] 
    @parent = Comment.find_by_id(params[:comment_id]) if params[:comment_id] 

    redirect_to root_path unless defined?(@parent) 
    end 
end 

ポストモデル:

has_many :comments, as: :commentable 
belongs_to :user 

def post 
    commentable.is_a?(Post) ? commentable : commentable.post 
    end 

コメントモデル:

belongs_to :user 

belongs_to :commentable, polymorphic: true 
has_many :comments, :as => :commentable 

路線:

resources :posts do 
    resources :comments 
    end 

    resources :comments do 
    resources :comments 
    end 

post_show.html.erb

アプリで

GitHubのレポ:https://github.com/Dmitry96/dasasd

答えて

0

はあなたのnew形式はどちらpost_idcomment_idパラメータを渡しません。これは、フォームのアクションURLまたはフォームの本文にeatherでなければなりません。

私はすべての画像を見ることはできませんが、私はフォームのアクションURLに親のIDを追加する必要があると思います。現在は/commentsで、親IDパラメータはありません。 /posts/:post_id/commentsまたは/comments/:comment_id/commentsである必要があります。

がにフォームを変更し

:仲間ねえ

<%= form_for [@parent, @comment] do |f| %> 
    <%= f.input :title %> 
    <%= f.text_area :body %> 
    <%= f.submit %> 
<% end %> 
+0

、ありがとうございました。私はこのフォームとコントローラで(comment_params)に切り替えました。それは働き始める – ApecBrah

関連する問題