回应json奇怪

时间:2011-03-07 19:34:30

标签: ruby-on-rails ruby-on-rails-3

 respond_to :json

  def create
    respond_with('test')
  end

我刚做了一些测试,上面给出了一个错误:

的未定义方法`test_url'

为什么要将测试变成网址?这是一个错误吗?

2 个答案:

答案 0 :(得分:2)

JSON对象必须有两个八位字节,如键和值。你不能只通过字符串发送。

请改为尝试:

respond_with(:message => "test")

答案 1 :(得分:1)

当test是一个类而我的路由没有包含资源条目时,我遇到了类似的错误(respond_with抛出了一个未定义的方法`test_url'错误)。

respond_to :json

def method
  if request.post?
    @test = Test.create
    respond_with @test
  end
  ...
end

向我的routes.rb添加资源条目解决了问题

resources :test
相关问题