Ruby on rails 如何使用Rails4应用程序的一些方法,如RESTAPI

Ruby on rails 如何使用Rails4应用程序的一些方法,如RESTAPI,ruby-on-rails,rest,api,authentication,ruby-on-rails-4,Ruby On Rails,Rest,Api,Authentication,Ruby On Rails 4,我需要你的帮助 我有一个Rails 4应用程序,我需要使用一些方法(不是全部)像REST API一样响应移动设备。我使用has_secure_密码模型对用户进行身份验证,并使用::reset_会话保护_免受伪造,以保护应用程序免受CSRF攻击(仅在post请求中) 现在,我在一个移动应用程序中工作,它需要使用我的Rails 4应用程序的一些方法,我有点困惑,我需要做哪些更改才能让应用程序响应REST API之类的设备请求 请注意,也许您应该将移动应用程序使用的路线分开。像 api/v1/<

我需要你的帮助

我有一个Rails 4应用程序,我需要使用一些方法(不是全部)像REST API一样响应移动设备。我使用has_secure_密码模型对用户进行身份验证,并使用::reset_会话保护_免受伪造,以保护应用程序免受CSRF攻击(仅在post请求中)

现在,我在一个移动应用程序中工作,它需要使用我的Rails 4应用程序的一些方法,我有点困惑,我需要做哪些更改才能让应用程序响应REST API之类的设备请求


请注意,

也许您应该将移动应用程序使用的路线分开。像

api/v1/<controller>/<methods>/

您可以看到这个gem

也许您应该将移动应用程序使用的路线分开。像

api/v1/<controller>/<methods>/

你可以看到这个宝石,它不应该太复杂。我可能会将这些移动API路由放在它们自己的名称空间中,以帮助组织。通常,移动应用程序与之交互的每个表都有一个单独的控制器

下面是我在需要一个新的REST控制器时使用的一个样板的示例。希望它能帮助你

# -------------------------------------------------------------------------------------
#    ___                      ___          _
#   / __\___  _ __  _   _    / _ \__ _ ___| |_ __ _
#  / /  / _ \| '_ \| | | |  / /_)/ _` / __| __/ _` |
# / /__| (_) | |_) | |_| | / ___/ (_| \__ \ || (_| |
# \____/\___/| .__/ \__, | \/    \__,_|___/\__\__,_|
#            |_|    |___/
# -------------------------------------------------------------------------------------
# This is a template controller which represents what most admin controllers look like.
# -------------------------------------------------------------------------------------
# Search for Camel and replace it with your camel case object (i.e. User)
# Search for snake and replace it with your snake case object (i.e. user)
# When search/replacting be sure to not include the 's'. This will handle plural/singular
# -------------------------------------------------------------------------------------
module V1
  module Admin
    class CamelsController < ApplicationController

      # GET /admin/snakes
      def index
        if admin_can :snake_read
          render json: Camel.all, each_serializer: Adm::CamelSerializer
        end
      end

      # GET /admin/snakes/:id
      def show
        if admin_can :snake_read
          render json: @snake, serializer: Adm::CamelSerializer
        end
      end

      # POST /admin/snakes
      def create
        if admin_can :snake_create
          @snake = Camel.new(snake_params.merge!(creator: current_user))

          if @snake.save
            render json: @snake, serializer: Adm::CamelSerializer, status: :created
          else
            render json: { errors: @snake.errors }, status: :unprocessable_entity
          end
        end
      end

      # GET /admin/snakes/new
      def new
        if admin_can :snake_create
          render json: Camel.new, new: true, serializer: Adm::CamelSerializer, root: "snake"
        end
      end

      # GET /admin/snakes/:id/edit
      def edit
        if admin_can :snake_update
          render json: @snake, edit: true, snake_id: params[:id], serializer: Adm::CamelSerializer, root: "snake"
        end
      end

      # PATCH /admin/snakes/:id
      def update
        if admin_can :snake_update
          if @snake.update(snake_params)
            head :no_content
          else
            render json: { errors: @snake.errors }, status: :unprocessable_entity
          end
        end
      end

      # DELETE /admin/snakes/:id
      def destroy
        if admin_can :snake_delete
          @snake.destroy
          head :no_content
        end
      end

      private
      def snake_params
        params.require(:snake).permit(:name).merge!(updater: current_user)
      end

    end
  end
end
#-------------------------------------------------------------------------------------
#    ___                      ___          _
#   / __\___  _ __  _   _    / _ \__ _ ___| |_ __ _
#  / /  / _ \| '_ \| | | |  / /_)/ _` / __| __/ _` |
# / /__| (_) | |_) | |_| | / ___/ (_| \__ \ || (_| |
# \____/\___/| .__/ \__, | \/    \__,_|___/\__\__,_|
#            |_|    |___/
# -------------------------------------------------------------------------------------
#这是一个模板控制器,它代表了大多数管理控制器的外观。
# -------------------------------------------------------------------------------------
#搜索Camel并将其替换为Camel case对象(即用户)
#搜索snake并将其替换为snake case对象(即用户)
#搜索/重播时,请确保不包含“s”。这将处理复数/单数
# -------------------------------------------------------------------------------------
模块V1
模块管理员
类CamelsController

本例假设您使用的是活动模型序列化程序,这使得JSON的呈现更加简单。

这应该不会太复杂。我可能会将这些移动API路由放在它们自己的命名空间中,以帮助组织。您的移动应用程序与之交互的每个表通常都有一个单独的控制器

下面是一个当我需要一个新的REST控制器时使用的样板文件的例子。希望它能帮助你

# -------------------------------------------------------------------------------------
#    ___                      ___          _
#   / __\___  _ __  _   _    / _ \__ _ ___| |_ __ _
#  / /  / _ \| '_ \| | | |  / /_)/ _` / __| __/ _` |
# / /__| (_) | |_) | |_| | / ___/ (_| \__ \ || (_| |
# \____/\___/| .__/ \__, | \/    \__,_|___/\__\__,_|
#            |_|    |___/
# -------------------------------------------------------------------------------------
# This is a template controller which represents what most admin controllers look like.
# -------------------------------------------------------------------------------------
# Search for Camel and replace it with your camel case object (i.e. User)
# Search for snake and replace it with your snake case object (i.e. user)
# When search/replacting be sure to not include the 's'. This will handle plural/singular
# -------------------------------------------------------------------------------------
module V1
  module Admin
    class CamelsController < ApplicationController

      # GET /admin/snakes
      def index
        if admin_can :snake_read
          render json: Camel.all, each_serializer: Adm::CamelSerializer
        end
      end

      # GET /admin/snakes/:id
      def show
        if admin_can :snake_read
          render json: @snake, serializer: Adm::CamelSerializer
        end
      end

      # POST /admin/snakes
      def create
        if admin_can :snake_create
          @snake = Camel.new(snake_params.merge!(creator: current_user))

          if @snake.save
            render json: @snake, serializer: Adm::CamelSerializer, status: :created
          else
            render json: { errors: @snake.errors }, status: :unprocessable_entity
          end
        end
      end

      # GET /admin/snakes/new
      def new
        if admin_can :snake_create
          render json: Camel.new, new: true, serializer: Adm::CamelSerializer, root: "snake"
        end
      end

      # GET /admin/snakes/:id/edit
      def edit
        if admin_can :snake_update
          render json: @snake, edit: true, snake_id: params[:id], serializer: Adm::CamelSerializer, root: "snake"
        end
      end

      # PATCH /admin/snakes/:id
      def update
        if admin_can :snake_update
          if @snake.update(snake_params)
            head :no_content
          else
            render json: { errors: @snake.errors }, status: :unprocessable_entity
          end
        end
      end

      # DELETE /admin/snakes/:id
      def destroy
        if admin_can :snake_delete
          @snake.destroy
          head :no_content
        end
      end

      private
      def snake_params
        params.require(:snake).permit(:name).merge!(updater: current_user)
      end

    end
  end
end
#-------------------------------------------------------------------------------------
#    ___                      ___          _
#   / __\___  _ __  _   _    / _ \__ _ ___| |_ __ _
#  / /  / _ \| '_ \| | | |  / /_)/ _` / __| __/ _` |
# / /__| (_) | |_) | |_| | / ___/ (_| \__ \ || (_| |
# \____/\___/| .__/ \__, | \/    \__,_|___/\__\__,_|
#            |_|    |___/
# -------------------------------------------------------------------------------------
#这是一个模板控制器,它代表了大多数管理控制器的外观。
# -------------------------------------------------------------------------------------
#搜索Camel并将其替换为Camel case对象(即用户)
#搜索snake并将其替换为snake case对象(即用户)
#搜索/重播时,请确保不包含“s”。这将处理复数/单数
# -------------------------------------------------------------------------------------
模块V1
模块管理员
类CamelsController