respond_with 实际上是用于 withActiveModel
的实例。我尝试将它与OpenStruct
's 实例一起使用,但它会引发错误。是否可以将 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:in
handle_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:in
polymorphic_url'