在 Rails 5 API 中渲染视图 [英] Render a view in Rails 5 API
问题描述
我通过 rails new
.我决定包含一个用于测试某些内容的视图,但在加载视图时遇到了问题.
I generated an API-only rails app with Rails 5 via rails new <application-name> --api
. I've decided I want to include a view for testing some things and am having issues getting a view to load.
我用一些文本创建了一个 users/index.html.erb 文件,我的控制器现在只是 def index;结束
但是当我点击/users URL 时什么也没有出现.我还尝试注释掉 config/application.rb 中的 # config.api_only = true
但这没有任何影响.有关如何进行的任何建议?
I created a users/index.html.erb file with some text and my controller is now simply def index; end
but there is nothing appearing when I hit the /users URL. I also tried commenting out the # config.api_only = true
in config/application.rb but that didn't affect anything. Any suggestions on how to proceed?
推荐答案
为此您无需取消注释 config.api_only = true
,只需从 ActionController 继承您的控制器即可::Base
,或在您的 ApplicationController
中执行(默认用于通用导轨生成).
You don't need to uncomment config.api_only = true
for this purpose, just inherit your controller from ActionController::Base
, or do it in your ApplicationController
(default for common rails generation).
代码:
- 仅对于此控制器
YourController <;ActionController::Base
- 对于所有应用程序
ApplicationController
这篇关于在 Rails 5 API 中渲染视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!