I am trying to allow users to reply through comments by allowing users to click a reply link next to the parent comment. It will send in a parameter to the 'new' comment view, like so:
> <%= link_to "Reply", new_comment_path(:in_reply_to => comment.id) %>
The form will use the :in_reply_to parameter in order to save that id as the parent id, like so:
comments_controller#new: @comment_parent = Comment.find(params[:in_reply_to])
comments_form view: <%= form_for([@comment]) do |f| %> <%#= render 'shared/error_messages', :object => f.object %> <div class="field"> <%= f.label :title %><br /> <%= f.text_field :title %> </div> <div class="field"> <%= f.label :content %><br /> <%= f.text_area :content %> </div> <%= f.hidden_field :parent_id, :value => @comment_parent.id %> <div class="actions"> <%= f.submit "Post Comment" %> </div> <% end %>
So, in the create controller, I want to create a new comment based on the parameters, and then find the parent comment by looking up the parent_id in Comment.find, but it cannot find any comment with that ID.
comments_controller#create @comment = Comment.new(params[:comment]) @comment.user_id = current_user.id @comment.save! @comment_parent = Comment.find(params[:parent_id]) # cannot find a comment with this ID? @comment_parent.children << @comment if @comment.save flash[:success] = "Comment saved." @comment_parent.save redirect_to @comment else flash[:error] = "Error in creating comment." # @comments = @commentable.comments.paginate(:page => params[:page]) render 'new'
I get this error: Couldn't find Comment without an ID, from this line: @comment_parent = Comment.find(params[:parent_id])
I also try @comment_parent = Comment.find(@comment.parent_id), but I get the same error.
Thank you.