在 Rails 5 API 中渲染视图 [英] Render a view in Rails 5 API

查看:31
本文介绍了在 Rails 5 API 中渲染视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过 rails new 用 Rails 5 生成了一个仅限 API 的 rails 应用程序.--api.我决定包含一个用于测试某些内容的视图,但在加载视图时遇到了问题.

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).

代码:

  1. 仅对于此控制器 YourController <;ActionController::Base
  2. 对于所有应用程序 ApplicationController

这篇关于在 Rails 5 API 中渲染视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆