读书人

请问rails异常: No route matches [G

发布时间: 2012-03-01 10:25:46 作者: rapoo

请教rails错误: No route matches [GET]
ruby on rails版本:3.1

routes.rb

C/C++ code
Demo::Application.routes.draw do  root :to => 'index#index'  match ':controller(/:action(/:id(.:format)))'end


MessagesController.rb
C/C++ code
class MessagesController < ApplicationController  def index    @messages = Message.page(params[:page]).per(5)    @message = Message.new        respond_to do |format|      format.html      format.json { render :json => @messages.to_json }    end  endend


浏览:http://127.0.0.1:3000/messages/index.json
报错:No route matches [GET] "/messages/index.json"

浏览http://127.0.0.1:3000/messages/index正常

求解,谢谢。

[解决办法]
你必需得用application/json浏览器头去请求这个URL才会输出JSON格式的信息,而且后边不要加.json
它输出什么是根据你发送了什么样的头信息来决定的,比如你还定义有:

format.xml {render :xml => ....}

那么用text/xml头去请求便见效果。在浏览器地址栏直接输入是不行的,那样的头是text/html头。

读书人网 >Ruby Rails

热点推荐