【发布时间】:2016-04-02 17:23:18
【问题描述】:
respond_with 实际上是用于ActiveModel 的实例。我尝试将它与OpenStruct 的实例一起使用,但它会引发错误。
是否可以将 respond_with 与自定义对象一起使用?
class CryptController < ApplicationController
respond_to :json
def my_action
respond_with OpenStruct.new(foo: 'foo', bar: 'bar')
end
# ...
end
引发:**未定义的方法persisted?' for nil:NilClass**
ruby-2.1.4@rails4/gems/actionpack-4.2.5.1/lib/action_dispatch/routing/polymorphic_routes.rb:298:inhandle_list'
/home/workstat/.rvm/gems/ruby-2.1.4@rails4/gems/actionpack-4.2.5.1/lib/action_dispatch/routing/polymorphic_routes.rb:206:in polymorphic_method'
/home/workstat/.rvm/gems/ruby-2.1.4@rails4/gems/actionpack-4.2.5.1/lib/action_dispatch/routing/polymorphic_routes.rb:114:inpolymorphic_url'
【问题讨论】:
-
你能先显示你的代码和错误吗?
respond_with也适用于 AR 对象。 -
@ArupRakshit 我已经更新了问题
标签: ruby-on-rails ruby responders