我刚接触过rails并寻求一些帮助,我已经在圈子中转了一段时间,当我运行localHost:3000时,我收到以下错误,路由错误未初始化的常量ArticleController,我一直在编辑routes.rb为单数复数但似乎导致了更多问题
routes.rb
Rails.application.routes.draw do
resources :articles, :comments
root 'articles#index'
resources :people
get 'entries/sign_in' => 'entries#sign_in'
post 'entries/sign_in' => 'entries#sign_in'
get 'assignment_2/index' => 'assignment#index'
end
我的文章控制器如下
article_controller
class ArticleController < ApplicationController
def index
@article = Article.page(params[:page]).per(25)
end
def update
@article = Article.find(params[:id])
if @article.update(params.require(:article).permit(:title, :company, :url))
redirect_to root_path
else
render 'edit'
end
end
def create
@article = Article.new(params.require(:article).permit(:title, :company, :url))
if @article.save
redirect_to root_path
else
render 'new'
end
end
def show
@article = Article.page(params[:page]).per(25)
end
def new
@article = Article.new
end
end
实际错误显示为
NameError in ArticlesController#index
uninitialized constant ArticlesController::Articles
Extracted source (around line #3):
1 class ArticlesController < ApplicationController
2 def index
3 @article = Articles.page(params[:page]).per(25)
4 end
5 def update
6 @article = Articles.find(params[:id])
Rails.root: C:/Users/markf/Desktop/School/RAD/RAD_Assignment_2_s9971085/Assignment_2_app
Application Trace | Framework Trace | Full Trace
app/controllers/articles_controller.rb:3:in `index'
actionpack (5.1.6) lib/action_controller/metal/basic_implicit_render.rb:4:in `send_action'
actionpack (5.1.6) lib/abstract_controller/base.rb:186:in `process_action'
actionpack (5.1.6) lib/action_controller/metal/rendering.rb:30:in `process_action'
actionpack (5.1.6) lib/abstract_controller/callbacks.rb:20:in `block in process_action'
activesupport (5.1.6) lib/active_support/callbacks.rb:131:in `run_callbacks'
actionpack (5.1.6) lib/abstract_controller/callbacks.rb:19:in `process_action'
actionpack (5.1.6) lib/action_controller/metal/rescue.rb:20:in `process_action'
actionpack (5.1.6) lib/action_controller/metal/instrumentation.rb:32:in `block in process_action'
activesupport (5.1.6) lib/active_support/notifications.rb:166:in `block in instrument'
activesupport (5.1.6) lib/active_support/notifications/instrumenter.rb:21:in `instrument'
activesupport (5.1.6) lib/active_support/notifications.rb:166:in `instrument'
actionpack (5.1.6) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
actionpack (5.1.6) lib/action_controller/metal/params_wrapper.rb:252:in `process_action'
activerecord (5.1.6) lib/active_record/railties/controller_runtime.rb:22:in `process_action'
actionpack (5.1.6) lib/abstract_controller/base.rb:124:in `process'
actionview (5.1.6) lib/action_view/rendering.rb:30:in `process'
actionpack (5.1.6) lib/action_controller/metal.rb:189:in `dispatch'
actionpack (5.1.6) lib/action_controller/metal.rb:253:in `dispatch'
actionpack (5.1.6) lib/action_dispatch/routing/route_set.rb:49:in `dispatch'
actionpack (5.1.6) lib/action_dispatch/routing/route_set.rb:31:in `serve'
actionpack (5.1.6) lib/action_dispatch/journey/router.rb:50:in `block in serve'
actionpack (5.1.6) lib/action_dispatch/journey/router.rb:33:in `each'
actionpack (5.1.6) lib/action_dispatch/journey/router.rb:33:in `serve'
actionpack (5.1.6) lib/action_dispatch/routing/route_set.rb:844:in `call'
rack (2.0.5) lib/rack/etag.rb:25:in `call'
rack (2.0.5) lib/rack/conditional_get.rb:25:in `call'
rack (2.0.5) lib/rack/head.rb:12:in `call'
rack (2.0.5) lib/rack/session/abstract/id.rb:232:in `context'
rack (2.0.5) lib/rack/session/abstract/id.rb:226:in `call'
actionpack (5.1.6) lib/action_dispatch/middleware/cookies.rb:613:in `call'
activerecord (5.1.6) lib/active_record/migration.rb:556:in `call'
actionpack (5.1.6) lib/action_dispatch/middleware/callbacks.rb:26:in `block in call'
activesupport (5.1.6) lib/active_support/callbacks.rb:97:in `run_callbacks'
actionpack (5.1.6) lib/action_dispatch/middleware/callbacks.rb:24:in `call'
actionpack (5.1.6) lib/action_dispatch/middleware/executor.rb:12:in `call'
actionpack (5.1.6) lib/action_dispatch/middleware/debug_exceptions.rb:59:in `call'
web-console (3.6.2) lib/web_console/middleware.rb:135:in `call_app'
web-console (3.6.2) lib/web_console/middleware.rb:30:in `block in call'
web-console (3.6.2) lib/web_console/middleware.rb:20:in `catch'
web-console (3.6.2) lib/web_console/middleware.rb:20:in `call'
actionpack (5.1.6) lib/action_dispatch/middleware/show_exceptions.rb:31:in `call'
railties (5.1.6) lib/rails/rack/logger.rb:36:in `call_app'
railties (5.1.6) lib/rails/rack/logger.rb:24:in `block in call'
activesupport (5.1.6) lib/active_support/tagged_logging.rb:69:in `block in tagged'
activesupport (5.1.6) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (5.1.6) lib/active_support/tagged_logging.rb:69:in `tagged'
railties (5.1.6) lib/rails/rack/logger.rb:24:in `call'
sprockets-rails (3.2.1) lib/sprockets/rails/quiet_assets.rb:13:in `call'
actionpack (5.1.6) lib/action_dispatch/middleware/remote_ip.rb:79:in `call'
actionpack (5.1.6) lib/action_dispatch/middleware/request_id.rb:25:in `call'
rack (2.0.5) lib/rack/method_override.rb:22:in `call'
rack (2.0.5) lib/rack/runtime.rb:22:in `call'
activesupport (5.1.6) lib/active_support/cache/strategy/local_cache_middleware.rb:27:in `call'
actionpack (5.1.6) lib/action_dispatch/middleware/executor.rb:12:in `call'
actionpack (5.1.6) lib/action_dispatch/middleware/static.rb:125:in `call'
rack (2.0.5) lib/rack/sendfile.rb:111:in `call'
railties (5.1.6) lib/rails/engine.rb:522:in `call'
puma (3.11.4) lib/puma/configuration.rb:225:in `call'
puma (3.11.4) lib/puma/server.rb:632:in `handle_request'
puma (3.11.4) lib/puma/server.rb:446:in `process_client'
puma (3.11.4) lib/puma/server.rb:306:in `block in run'
puma (3.11.4) lib/puma/thread_pool.rb:120:in `block in spawn_thread'
Request
Parameters:
None
Toggle session dump
Toggle env dump
Response
Headers:
None
上面是在我重命名articles_controller之前,错误信息显示如下
ActionController::UnknownFormat in ArticlesController#index
ArticlesController#index is missing a template for this request format and variant. request.formats: ["text/html"] request.variant: [] NOTE! For XHR/Ajax or API requests, this action would normally respond with 204 No Content: an empty white screen. Since you're loading it in a web browser, we assume that you expected to actually render a template, not nothing, so we're showing an error to be extra-clear. If you expect 204 No Content, carry on. That's what you'll get from an XHR or API request. Give it a shot.
提取的来源(第53行):
51
52
53
54
55
56
"That's what you'll get from an XHR or API request. Give it a shot."
raise ActionController::UnknownFormat, message
else
logger.info "No template found for #{self.class.name}\##{action_name}, rendering head :no_content" if logger
super
Rails.root: C:/Users/markf/Desktop/School/RAD/RAD_Assignment_2_s9971085/Assignment_2_app
任何帮助都会非常感激,就像我说的还在学习。
答案 0 :(得分:0)
您的控制器名称错误。
将app/controllers/article_controller.rb
重命名为app/controller/articles_controller.rb
将行class ArticleController < ApplicationController
更改为
class ArticlesController < ApplicationController
此外,您的日志输出与articles_controller.rb
文件之间存在差异。
您应该使用Article
而不是Articles
。
模型名称以单数形式使用,控制器名称以复数形式使用。
因此,控制器将articles_controller
但模型将为Article
。
答案 1 :(得分:0)
控制器应始终采用复数形式。如果您运行bundle exec rake routes
,您会看到rails正在寻找像articles#index
这样的方法:
/config/routes.rb
resources articles
=> rake routes
Prefix Verb URI Pattern Controller#action
articles GET /articles(.:format) articles#index
POST /articles(.:format) articles#create
new_article GET /articles/new(.:format) articles#new
edit_article GET /articles/:id/edit(.:format) articles#edit
article GET /articles/:id(.:format) articles#show
PUT /articles/:id(.:format) articles#update
DELETE /articles/:id(.:format) articles#destroy
因此,这些更改应解决问题:
- 重命名控制器文件article_controller.rb
- &gt; articles_controller.rb
;
- 重命名控制器类名ArticleController
- &gt; ArticlesController
。