rails3-amf-在ActiveRecord对象上找不到to_amf方法
||
我在我的Rails 3 / Flex 4项目中使用warhammerkid的rails3-amf gem。
AFAIK,我正确地遵循了GitHub页面上的\“ Getting Started \”说明。
我已经将宝石行添加到我的Gemfile中。
我已使用
bundle install
安装了宝石。
在我的Flex应用程序中,我将对ManageMySchool::GradesController
文件中的index
动作进行RemoteObject调用。这是app/controllers/manage_my_school/grades_controller.rb
文件中的代码:
class ManageMySchool::GradesController < ApplicationController
respond_to :html, :amf
def index
@grade = Grade.first
respond_with(@grade) do |format|
format.amf { render :amf => @grade.to_amf }
end
end
end
在Rails项目(app/models/Grade.rb
)和Flex项目(Grade.as
,其中RemoteAlias
设置为Grade
)中,要序列化的模型的名称都称为Grade
。在config/application.rb
文件中,我以这种方式完成了类映射:
config.rails3amf.class_mapping do |m|
m.map :as => \'Grade\', :ruby => \'Grade\'
end
我已经通过这种方式完成了参数映射:
config.rails3amf.map_params :controller => \'ManageMySchool::GradesController\', :action => \'index\', :params => [:authenticity_token]
问题
现在,当我运行服务器并从Flex调用RemoteObject时,对于Grade
模型,我得到了to_amf undefined method
错误。
如果我将Grade.first
更改为Grade.all
,则@grade
会有一系列成绩。但是undefined method
错误消息仍然提及成绩模型。这意味着to_amf
方法适用于Array类,但不适用于ActiveRecord模型。
为什么是这样?我究竟做错了什么?
我需要为ActiveRecord模型“启用” rails3-amf gem做些什么吗?
我将不胜感激。谢谢!
更新资料
@warhammerkid:这是在rails控制台中看到的Grade.ancestors的输出。
ree-1.8.7-2011.03 :006 > puts Grade.ancestors
Grade
ActiveRecord::Base
Paperclip::CallbackCompatability::Rails3::Running
Paperclip::CallbackCompatability::Rails3
Paperclip::Glue CanCan::ModelAdditions
Authlogic::ActsAsAuthentic::ValidationsScope
Authlogic::ActsAsAuthentic::SingleAccessToken
Authlogic::ActsAsAuthentic::SessionMaintenance
Authlogic::ActsAsAuthentic::RestfulAuthentication::InstanceMethods
Authlogic::ActsAsAuthentic::RestfulAuthentication
Authlogic::ActsAsAuthentic::PersistenceToken
Authlogic::ActsAsAuthentic::PerishableToken
Authlogic::ActsAsAuthentic::Password
Authlogic::ActsAsAuthentic::MagicColumns
Authlogic::ActsAsAuthentic::Login
Authlogic::ActsAsAuthentic::LoggedInStatus
Authlogic::ActsAsAuthentic::Email
Authlogic::ActsAsAuthentic::Base
ActiveRecord::Aggregations
ActiveRecord::Transactions
ActiveRecord::Reflection
ActiveRecord::Serialization
ActiveModel::Serializers::Xml
ActiveModel::Serializers::JSON
ActiveModel::Serialization
ActiveRecord::AutosaveAssociation
ActiveRecord::NestedAttributes
ActiveRecord::Associations
ActiveRecord::AssociationPreload
ActiveRecord::NamedScope
ActiveModel::Validations::Callbacks
ActiveRecord::Callbacks
ActiveModel::Observing
ActiveRecord::Timestamp
ActiveModel::MassAssignmentSecurity
ActiveRecord::AttributeMethods::Dirty
ActiveModel::Dirty
ActiveRecord::AttributeMethods::TimeZoneConversion
ActiveRecord::AttributeMethods::PrimaryKey
ActiveRecord::AttributeMethods::Read
ActiveRecord::AttributeMethods::Write
ActiveRecord::AttributeMethods::BeforeTypeCast
#<Module:0x1028356f0> ActiveRecord::AttributeMethods::Query
ActiveRecord::AttributeMethods
ActiveModel::AttributeMethods
ActiveRecord::Locking::Optimistic
ActiveRecord::Locking::Pessimistic
ActiveRecord::Validations
ActiveModel::Validations::HelperMethods
ActiveModel::Validations
ActiveSupport::Callbacks
ActiveModel::Conversion
ActiveRecord::Persistence Object
PP::ObjectMixin Base64::Deprecated
Base64
ActiveSupport::Dependencies::Loadable
Kernel
注意只有only21ѭ在那里。没有提到Rails3AMF
。
这是否意味着我必须做一些特殊的事情才能为ActiveRecord模型加载Rails3AMF模块?
我正在使用最新版本的ree的Rails 3.0.5。这些宝石都包含在使用rvm管理的一个宝石集中。
更新2
如果删除render :amf
行中的to_amf
,则出现以下错误:
Grade Load (0.3ms) SELECT `grades`.* FROM `grades` LIMIT 1
Completed 200 OK in 195ms (Views: 0.1ms | ActiveRecord: 0.8ms)
Sending back AMF
NoMethodError (You have a nil object when you didn\'t expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.last):
Rendered /Users/anjan/.rvm/gems/ree-1.8.7-2011.03@rb/gems/actionpack-3.0.5/lib/> > action_dispatch/middleware/templates/rescues/_trace.erb (1.1ms)
Rendered /Users/anjan/.rvm/gems/ree-1.8.7-2011.03@rb/gems/actionpack-3.0.5/lib/> > action_dispatch/middleware/templates/rescues/_request_and_response.erb (2.8ms)
Rendered /Users/anjan/.rvm/gems/ree-1.8.7-2011.03@rb/gems/actionpack-3.0.5/lib/> > action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (13.6ms)
Started POST \"/amf\" for 127.0.0.1 at Fri Apr 15 17:03:34 +0530 2011
Sending back AMF
更新3
如果我在Grade.rb
模型文件的顶部手动添加include Rails3AMF::Serialization
行,那么一切都正常。那么,这是否意味着我必须在所有模型中都添加这条线?
我看到这已经在gem的lib/rails3-amf/serialization.rb
文件的40-42行中完成了:
# Hook into any object that includes ActiveModel::Serialization
module ActiveModel::Serialization
include Rails3AMF::Serialization
end
为什么这不起作用?我应该在应用程序初始化时强制加载gem吗?
谢谢!
更新4-解决此问题的方法
好的,我最终在初始化程序中添加了以下代码块:
class ActiveRecord::Base
include Rails3AMF::Serialization
end
它正在工作。
@warhammerkid-感谢您的帮助。
没有找到相关结果
已邀请:
1 个回复
屡倒雷图