在ruby中为控制器路由

时间:2014-04-13 18:49:01

标签: ruby-on-rails ruby routing

我正在尝试构建一个简单的rails应用程序,但是我遇到了路由错误。这是控制器:

class PostsController < ActionController::Base

  def index

    @var = "Rails is amazing"

  end



end

这是路由:

get "/posts", to: "posts#index"

路由错误如下:

uninitialized constant PostsController

我正在访问的网址是:

http://localhost:3000/posts#

我知道控制器应该在路由和文件名中复数。对于这样一个新手问题我很抱歉

1 个答案:

答案 0 :(得分:1)

我相信,你在controllers文件夹中有posts_controller.rb文件。在posts_controller.rb文件中添加以下语法

class PostsController < ApplicationController

end

在路线文件中,尝试添加

resources :routes

如果您要输入CONTROLLER=posts rake routes,在终端中,您将获得以下输出

Prefix Verb   URI Pattern               Controller#Action
    posts GET    /posts(.:format)          posts#index
          POST   /posts(.:format)          posts#create
 new_post GET    /posts/new(.:format)      posts#new
edit_post GET    /posts/:id/edit(.:format) posts#edit
     post GET    /posts/:id(.:format)      posts#show
          PATCH  /posts/:id(.:format)      posts#update
          PUT    /posts/:id(.:format)      posts#update
          DELETE /posts/:id(.:format)      posts#destroy