活动模型序列化器-路由助手的未定义方法

Kyl*_*lin 1 active-model-serializers

我正在用rails-apigem 构建一个新的应用程序。我用active_model_serializers来自定义JSON响应。在尝试向url序列化器添加属性时,出现错误,指示资源的路由帮助器方法未定义。要遵循的相关代码:

# Routes
Rails.application.routes.draw do
  namespace :api, defaults: { format: 'json' } do
    namespace :v1 do
      resources :tasks, except: [:new, :edit]
    end
  end
end

# Controller - api/v1/tasks_controller.rb
class Api::V1::TasksController < ApplicationController
  before_action :set_task, only: [:show, :update, :destroy]

  # GET /tasks
  # GET /tasks.json
  def index
    @tasks = Task.all

    render json: @tasks
  end

  # GET /tasks/1
  # GET /tasks/1.json
  def show
    render json: @task
  end

  # POST /tasks
  # POST /tasks.json
  def create
    @task = Task.new(task_params)

    if @task.save
      render json: @task, status: :created, location: api_v1_task_path(@task.id)
    else
      render json: @task.errors, status: :unprocessable_entity
    end
  end

  # PATCH/PUT /tasks/1
  # PATCH/PUT /tasks/1.json
  def update
    @task = Task.find(params[:id])

    if @task.update(task_params)
      head :no_content
    else
      render json: @task.errors, status: :unprocessable_entity
    end
  end

  # DELETE /tasks/1
  # DELETE /tasks/1.json
  def destroy
    @task.destroy

    head :no_content
  end

  private

    def set_task
      @task = Task.find(params[:id])
    end

    def task_params
      params.require(:task).permit(:name)
    end
end

# Serializer
class TaskSerializer < ActiveModel::Serializer
  attributes :id, :name, :url

  def url
    api_v1_task_url(object)
  end
end
Run Code Online (Sandbox Code Playgroud)

如您所见,我正在使用命名空间路由并使控制器正常工作。undefined method 'api_v1_task_url尝试检索任务时收到错误消息。我不知道为什么不包括辅助方法。我感到困惑,因为我发现的有关添加此类自定义属性的每个教程都不会对使用这些gems(rails-api和active_model_serializers)进行任何调整。

这里是一些更相关的信息:rails版本:4.2.6 rails-api版本:0.4.0 active_model_serializers版本:0.10.0.rc5

编辑:这是输出rake routes

     Prefix Verb    URI Pattern                 Controller#Action
api_v1_tasks GET    /api/v1/tasks(.:format)     api/v1/tasks#index {:format=>"json"}
             POST   /api/v1/tasks(.:format)     api/v1/tasks#create {:format=>"json"}
 api_v1_task GET    /api/v1/tasks/:id(.:format) api/v1/tasks#show {:format=>"json"}
             PATCH  /api/v1/tasks/:id(.:format) api/v1/tasks#update {:format=>"json"}
             PUT    /api/v1/tasks/:id(.:format) api/v1/tasks#update {:format=>"json"}
             DELETE /api/v1/tasks/:id(.:format) api/v1/tasks#destroy {:format=>"json"}
Run Code Online (Sandbox Code Playgroud)

编辑2:按照以下建议,我尝试了:

# Serializer
class TaskSerializer < ActiveModel::Serializer
  attributes :id, :name, :url

  def url
    link(:self) { api_v1_task_url(object) }
  end
end
Run Code Online (Sandbox Code Playgroud)

并收到一个undefined method 'link'错误。

编辑3: @beauby确实指出我在错误的位置使用了此方法,但是正确使用它仍然无法通过序列化器方法创建属性,因为我在许多教程中都看到过这种工作,例如:http:// railscasts.com/episodes/409-active-model-serializers?view=asciicast。它根据linksJSONAPI架构创建一个属性,该属性虽然有用,但我会记住,但并不是我想要实现的。

Asc*_*hen 5

您可以添加Rails.application.routes.url_helpers到您的TaskSerializer使用url_helpers。

  class TaskSerializer < ActiveModel::Serializer
    include Rails.application.routes.url_helpers

    attributes :name

    link(:self) { api_v1_task_url(object) }
  end
Run Code Online (Sandbox Code Playgroud)

您还可以:json用作ActiveModel适配器并手动添加资源位置。

  class TaskSerializer < ActiveModel::Serializer
    include Rails.application.routes.url_helpers

    attributes :name, :resource_location

    def resource_location 
      api_v1_task_url(object)
    end
  end
Run Code Online (Sandbox Code Playgroud)