2011-11-27 108 views
2

我在使用Rails 3.0渲染JSON时遇到问题。每当我访问URL时,屏幕上都没有显示任何东西(我敢肯定JSON应该显示出类似于XML的形式吗?)对不起,我对Rails一般都比较陌生在Rails 3.0中渲染JSON

这是我的代码。

def rjson 
    @comments = Chat.all 

    respond_to do |format| 
     format.json { render :json => @comments } 
    end 
end 

我尽可能简化了它。根据一些教程我有

我的路由看起来是这样的:

match '/chatbox/rjson', :to => 'chatbox#rjson' 

我敢肯定,我的模型是好的。

我不知道我是否应该像'(rjson.json.erb?)'这样的'rjson'视图,但是我敢肯定,我只能从控制器渲染而没有视图吗?

回答

1

如果你确信rjson将只与json形式回应,你只需要:

def rjson 
    @comments = Chat.all 
    render :json => @comments 
end 
+0

噢噢噢,谢谢主席先生! – Vivek