0

嗨,我有一个类“listings_controller”。我在其中添加了一个方法“列表”,如下所示:

def index     
    @listings = Listing.order(:name)  
end  


def list  
    render :text=>(@listings).to_json  
end  

这是我的 routes.rb 文件

根到:'listings#index',作为:'listings'
资源:listings 做
集合做得到
:around
end
end

match ':controller(/:action(/:id))(.:format)'  

当我在浏览器中输入以下内容时:

http://localhost:3000/listings/list

我希望在浏览器中看到 JSON 对象及其内容。然而,这并没有发生。我想知道是否有人可以帮助我诊断我做错了什么。

webrick 服务器显示以下内容:

Started GET "/listings/list" for 127.0.0.1 at 2012-04-18 11:56:25 -0700  
Processing by ListingsController#show as HTML  
Parameters: {"id"=>"list"}  
Listing Load (0.3ms)  SELECT "listings".* FROM "listings" WHERE "listings"."id" = ? LIMIT 1  [["id", "list"]]  
Completed 500 Internal Server Error in 2ms  

ActiveRecord::RecordNotFound(找不到 id=list 的列表):
app/controllers/listings_controller.rb:104:in `show'

4

1 回答 1

0

在您的 routes.rb 中添加以下内容:

match 'listings/list' => 'listings#list'

或简写:

get 'listings/list'

目前它正在被路由到 show 方法。

于 2012-04-18T19:10:19.717 回答