ruby-on-rails - Rails 调用已删除的控制器
问题描述
这里的基本 Rails 问题:我有一个名为的控制器方法fetch_headlines
,我一直在尝试调试。我对控制器进行了更改,但它们没有反映在输出中。这让我觉得 Rails 没有正确调用fetch_headlines
. 观察下面的控制台。
Puma starting in single mode...
* Version 3.12.1 (ruby 2.6.1-p33), codename: Llamas in Pajamas
* Min threads: 5, max threads: 5
* Environment: development
* Listening on tcp://localhost:3000
Use Ctrl-C to stop
Started GET "/entries/fetch_headlines" for ::1 at 2020-01-08 16:03:03 -0500
(0.6ms) SELECT "schema_migrations"."version" FROM "schema_migrations" ORDER BY "schema_migrations"."version" ASC
↳ /Users/ed/.rvm/gems/ruby-2.6.1/gems/activerecord-5.2.3/lib/active_record/log_subscriber.rb:98
Processing by EntriesController#show as HTML
Parameters: {"id"=>"fetch_headlines"}
Rendering entries/show.html.erb within layouts/application
Rendered entries/show.html.erb within layouts/application (2.8ms)
Completed 200 OK in 482ms (Views: 476.3ms | ActiveRecord: 0.0ms)
此外,如果我请求不存在的方法,Rails 会呈现相同的行为,例如:qwertyqwerty
. 我希望 Rails 会抛出错误。
Started GET "/entries/qwertyqwerty" for ::1 at 2020-01-09 05:59:13 -0500
Processing by EntriesController#show as HTML
Parameters: {"id"=>"qwertyqwerty"}
Rendering entries/show.html.erb within layouts/application
Rendered entries/show.html.erb within layouts/application (0.6ms)
Completed 200 OK in 18ms (Views: 17.1ms | ActiveRecord: 0.0ms)
这是我的路线.rb
get 'fetch_headlines', to: 'entries#fetch_headlines'
resources :entries
resources :keywords
resources :networks
谢谢。
解决方案
你放get 'fetch_headlines', to: 'entries#fetch_headlines'
,它创建/fetch_headlines
而不是/entries/fetch_headlines
路由。你需要的是这样的:
get 'entries/fetch_headlines', to 'entries#fetch_headlines'
甚至更好
resources :entries do
collection do
get :fetch_headlines
end
end
推荐阅读
- sql - 如果 Column 的值不是 Oracle SQL 中提供的值,则获取该行
- spring-boot - Springboot 应用程序在尝试从 keycloak 访问令牌中获取角色时抛出异常
- java - 如何打印出类中每个对象的所有数据?
- javascript - Kendo DateTimePicker 从本地存储读取后没有时钟
- excel - VBA Excel:循环
- docker - Docker Container 时钟不同步且超前于系统时钟
- android - kivy 和 bs4 应用程序崩溃
- node.js - 存储桶上的云 ml 引擎读取访问权限
- linux - 通过文件管理器 (Dolphin) 共享 Samba
- bash - 如何使用从列表中获取的变量输入文件 - bash