【问题标题】:Wrong number of arguments Active Model Serializer活动模型序列化程序的参数数量错误
【发布时间】:2018-07-16 21:57:23
【问题描述】:

我将 AMS 的 0.10 分支与 Rails 5.2 应用程序一起使用,并尝试启动第一个序列化程序。我收到以下错误消息:

wrong number of arguments (given 2, expected 0..1)

在我的日志中:

NoMethodError (undefined method `symbolize_keys' for #<Site:0x00007f80fdad2fc0>):

与:

module Api 
  module V1
    class SitesController < ApplicationController
      def show 
        @site = Site.find(params[:id])
        render json: @site, serializer: SiteSerializer
      end
    end
  end
end

和一个序列化器:

class SiteSerializer < ActiveModelSerializers::Model
  attributes :id, :name
end

但我不知道为什么。

编辑#1

耙路线:

       api_v1_sites GET  /api/v1/sites(.:format)                                                                  api/v1/sites#index
       api_v1_site GET  /api/v1/sites/:id(.:format)                                                              api/v1/sites#show

编辑#2

查看日志,我得到了错误:

NoMethodError (undefined method `symbolize_keys' for #<Site:0x00007f80fdad2fc0>):

【问题讨论】:

  • 路线有什么?
  • render json: SiteSerializer.new(@site) 是做什么的?
  • 交换你的路线,以便它首先剃掉api_v1_sites GET /api/v1/sites(.:format) 然后api_v1_site GET /api/v1/sites/:id(.:format) 这条路线。

标签: ruby-on-rails active-model-serializers


【解决方案1】:
module Api 
  module V1
    class SitesController < ApplicationController
      def show 
        @site = Site.find(params[:id]).name
        render json: @site, serializer: SiteSerializer
      end
    end
  end
end

让我知道这是否有效?

【讨论】:

    猜你喜欢
    • 2016-01-23
    • 1970-01-01
    • 2023-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多