未定义的方法'已发布'

时间:2017-03-15 23:18:16

标签: ruby

我刚刚开始学习Ruby on Rails并获得了第一版第3章第151页的第7章。

我按如下方式生成控制器; $ rails生成控制器ControllerName [actions] [options]。这个工作正常,但当我试图为用户生成一个控制器 $ rails生成控制器用户。我收到以下错误消息。

  

/routing/mapper.rb:328:in check_part:缺少:路由定义上的控制器密钥,请检查您的路由。 (ArgumentErroe)。

这就是我的路线看起来像

Rails.application.routes.draw do
  get 'controller_name/[actions]'

  get 'controller_name/[options]'

  root :to => "articles#index"
  resources :articles
  root :to => 'users#show'

end

我添加了最后一条路线(root:to =>'users#show'

Stackoverflow社区非常棒。我从档案馆得到了很多帮助。

1 个答案:

答案 0 :(得分:1)

您看到的错误是由于以下行:

获取' controller_name / [actions]'

假设您要将GET localhost:3000/welcome路由到PagesController#welcome。您需要在路线中指定路径和控制器#动作:

get '/welcome', controller: 'pages#index'

我真的建议您阅读路由! Rails has a great guide on routing.

除此之外,您的代码中还有其他一些错误。这是一个带注释的版本:

Rails.application.routes.draw do
  # These two routes are invalid
  get 'controller_name/[actions]'
  get 'controller_name/[options]'

  # This routes the root of your site (localhost:3000/) 
  # to ArticlesController#index
  # 
  # This should be moved to the top of the file
  root :to => "articles#index"
  resources :articles

  # You can't have two root routes!
  root :to => 'users#show'
end

祝你好运,欢迎来到Rails!