0

Я хочу сериализации вновь созданный ресурс в этой акции:ActiveModelSerializer не ногами в

def create 
    @comment = @commentable.comments.build(comment_params) 
    @comment.user = current_user 
    respond_to do |format| 
     if @comment.save 
     format.json { render json: @comment } 
     format.html { redirect_to @question, notice: "Comment successfully created." } 
     else 

     format.html { render 'questions/show' } 
     end 
    end 
    end 

Но render json: @comment возвращает полный объект, хотя в моем CommentSerializer у меня есть:

class CommentSerializer < ActiveModel::Serializer 
    attributes :id 
end 

я создаю новый ресурс через ajax:

handleSubmit: function(e){ 
     e.preventDefault(); 
     $.ajax({ 
      context: this, 
      method: 'POST', 
      url: "https://stackoverflow.com/questions/" + this.props.question_id + "/comments", 
      data: {comment: this.state}, 
      success: function(data){ 
       this.props.handleNewComment(data); 
       this.setState(this.getInitialState); 
      } 
     }); 

    }, 

Что мне здесь не хватает?

ответ

0

вы можете явно задать сериалайзер при рендеринге JSON

изменение

format.json { render json: @comment } 

в

format.json { render json: CommentSerializer.new(@comment) } 
0

Я считаю, что вам не хватает contentType, поэтому добавьте contentType: "application/json" к вашей функции ajax.

+0

Запрос направляется только в порядке, ответ не сериализовано, так что это не причина. –

 Смежные вопросы

  • Нет связанных вопросов^_^