简体   繁体   中英

Postman problems in post request

I'm trying to use postman with a post request for mi controller "comments". This rquest show me the following error:

"NoMethodError (undefined method `news'..."

Here is the comment model:

    class Comment < ApplicationRecord
  include Discard::Model

  belongs_to :user, optional: true
  belongs_to :news, optional: true

  validates :news_id, presence: true
  validates :user_id, presence: true
  validates :body, presence: true
end

and here is the controller:

    module Api
  module V1
    class CommentsController < ApplicationController
      before_action :authenticate_request, only: %i[index create update destroy]
      before_action :set_comment, only: %i[update destroy]
      before_action :authorize_user, only: %i[show create update destroy]

      def index
        @comment = Comment.order('created_at DESC')
        render json: CommentSerializer.new(@comment,
                                            fields: { comment: :body })
                                       .serializable_hash, status: :ok
      end

      def create
        @comment = Comment.new(comment_params)
        @comment.user = @current_user
        @comment.news = @current_user.news.find(params[:news_id])
        if @comment.save
          render json: CommentsSerializer.new(@comment).serializable_hash, status: :created
        else
          render_error
        end
      end

      def update
        if ownership?
          @comment = Comment.update(comment_params)
          render json: CommentSerializer.new(@comment).serializable_hash
        else
          render json: @comment.errors, status: :unprocessable_entity
        end
      end

      def destroy
        if ownership?
          @comment.discard
          head :no_content
        else
          render json: { error: 'unauthorized' }, status: :unauthorized
        end
      end

      private

      def set_comment
        @comment = Comment.find(params[:id])
      end

      def comment_params
        params.require(:comment).permit(:body)
      end

      def render_error
        render json: { errors: @comment.errors.full_messages },
               status: :unprocessable_entity
      end

      def render_unauthorized
        render status: :unauthorized, json: {
          errors: [I18n.t('errors.controllers.unauthorized')]
        }
      end
    end
  end
end

When I send a request for post in postman, it show the following problem: NoMethodError (undefined method `news' for #<User id: 27, first_name: "rafa", last_name: "quinteros", email: "rafa4@email.com", password_digest: [FILTERED], photo: nil, role_id: 1, created_at: "2022-08-05 13:42:51.360055000 -0300", updated_at: "2022-08-05 13:42:51.360055000 -0300", discarded_at: nil>):

Well, your comment.user receives @current_user , but the user doesn't have news method/relationship. You should write a news method to stop this error.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM