Post#update 要能更新單筆文章

module ApiV0
  class Posts < Grape::API
    # ...

    desc "Update post"
    params do
      requires :id, type: String, desc: 'Post ID.'
      requires :title, type: String, desc: "Post title"
      requires :context, type: String, desc: "Post context"
    end
    patch "/posts/:id" do
      post = current_user.posts.find_by(id: params[:id])

      if post.update(declared(params, include_missing: false).except(:access_key))
        present post, with: ApiV0::Entities::Post
      else
        raise StandardError, $!
      end
    end

    # ...
  end
end
Copyright © NicLin 2019 all right reserved,powered by Gitbook該頁面生成時間: 2019-04-09 10:00:37

results matching ""

    No results matching ""