ruby-on-rails - Rails/Ember-active_model_serializer-侧面加载时未定义的方法 `object'

标签 ruby-on-rails ember.js active-model-serializers

我试图在Ember应用程序的active_model_serializer中加载数据,并在尝试包含对象时收到NoMethodError:

undefined method `object' for #Email:0x00000100d33d20



仅当设置:include => true时才会发生,如下所示:
class ContactSerializer < ActiveModel::Serializer
  embed :ids, :include => true
  attributes :first_name, :last_name
  has_many :emails
end

我的模型如下所示:
class Contact < ActiveRecord::Base
  attr_accessible :first_name, :last_name, :company,
  belongs_to :account
  belongs_to :user
  has_many :emails
end

class Email < ActiveRecord::Base
  attr_accessible :email_address, :email_type_id, :is_primary  
  belongs_to :contact
end

我的 Controller 如下所示:
def show
  @contact = @current_user.contacts.where(:id => params[:id]).includes(:emails).first
  render :json => @contact
end

提前致谢。

最佳答案

如上文Deefour所述,请确保您有一个用于任何侧载对象的序列化程序。在这种情况下,创建EmailSerializer:

class EmailSerializer < ActiveModel::Serializer
  attributes :id, :email_address
end

关于ruby-on-rails - Rails/Ember-active_model_serializer-侧面加载时未定义的方法 `object',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14106752/

相关文章:

ruby-on-rails - 如何将 has_one 关联限制为单个记录

javascript - 为什么要评估 if block

ruby-on-rails - 数组中不同类的 active_model_serializers

javascript - Ember 中的 RadioButtonList,将模型属性绑定(bind)到所选值

ruby-on-rails - 当对象保存失败时,以数据为根的 activemodel 序列化器

ruby-on-rails - 事件模型序列化器将所有日期转换为秒

javascript - Rails - 从 Controller 触发 javascript 事件

ruby-on-rails - M. Hartl 的 RoR 书中的练习

ember.js - 从 ember.js 主干风格触发自定义事件

docker - 无法启动运行 ember.js 镜像的 docker 容器,node_modules 为空