the-trash/the_comments

json format

Opened this issue · 3 comments

Hello.
How about json format for creation comment? I try to use the gem with angularjs and have many problem with it.

Thanks.

Hello! What do you mean when talking about json? Can you show some examples of what you want?

I mean about these methods

   # Protection hooks
    def ajax_requests_required
      unless request.xhr?
        return render(text: t('the_comments.ajax_requests_required'))
      end
    end

   def create
      @comment = @commentable.comments.new comment_params
      if @comment.valid?
        @comment.save
        return render layout: false, partial: comment_partial(:comment), locals: { tree: @comment }
      end
      render json: { errors: @comment.errors }
    end

I added to headers {'X-Requested-With': 'XMLHttpRequest'} and ajax_requests_required had been passed but the render method returned to me a html code.

I think it would be great if you add to config json and ajax formats and in the create method it will be checking by example below

 def create
    @comment = @commentable.comments.new comment_params
    if @comment.valid?
      @comment.save
      respond_to do |format|
        format.js{render layout: false, partial: comment_partial(:comment), locals: { tree: @comment }}
        format.json{ render json: @commentable.comments }
      end
    else
      render json: { errors: @comment.errors }
    end
  end

and also I think error should return 422 error. Now it returns 200.

Thanks.

@sunchess, thanks! Now I see.

  1. error should return 422 error. You abs right. It was my wrong. It should be fixed.

  2. if you add to config json and ajax formats. Yet another problem. I accept it.

Now I think, this method should return only JSON. In wrong case it should be

render json: { errors: @comment.errors }, status: 422

If it success case it should be

render json: { comment_html: comment_partial(:comment), locals: { tree: @comment } }
  1. You should know, I stopped support this gem because

a) I have second version of the gem with most advanced and clean code.
b) Right now, I have no time to improve it

I ready for PR if you have any useful ideas.
And Thank for your feedback!