如何将 Rails 5 API 应用程序转换为既可以充当 API 又可以充当应用程序的 Rails 应用程序? [英] How do you convert a Rails 5 API app to a rails app that can act as both API and app?

查看:37
本文介绍了如何将 Rails 5 API 应用程序转换为既可以充当 API 又可以充当应用程序的 Rails 应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最初在 rails 5 中使用 --api 标签创建了它.

I initially created it in rails 5 with the --api tag.

来自 http://edgeguides.rubyonrails.org/api_app.html

我删除了config.api_only = true

我变了

class ApplicationController < ActionController::API
end

class ApplicationController < ActionController::Base
end

我现在遇到的问题是视图何时呈现,例如.welcome/index.html.erb,对应的CSS文件assets/stylesheets/welcome.css.scss不是.

The problem I'm having now is when the view is getting rendered, ex. welcome/index.html.erb, the corresponding CSS file assets/stylesheets/welcome.css.scss is not.

知道如何解决这个问题,或者更一般地将 API 应用程序转换为完整的应用程序吗?

Any idea how I can fix this, or more generally convert the API application to a full app?

谢谢!

推荐答案

我遇到了同样的问题,我相信我已经解决了.我希望找到一个简单的 rails 生成器来转换它,但除非我错过了一些东西,否则这并不容易.然而,Rails 确实比完全手动操作更容易.

I ran into this same problem and I believe I've solved it. I was hoping to find a simple rails generator to convert it, but unless I've missed something it's not that easy. However, rails does make it easier than doing it totally manually.

关键是 rails new 命令可以在现有应用上使用.请注意,此答案假设您知道如何使用 git 并在现有应用上使用它.

The key is that the rails new command can be used on an existing app. Note that this answer assumes you know how to use git and are using it on the existing app.

首先,也是最重要的,创建一个新分支.这有两个功能,1)所以如果你搞砸了你不应该丢失你的工作(尽管它仍然可能是备份它的好时机,比如到 GitHub),和 2)所以你可以比较有在此过程之后发生冲突并检索此过程覆盖的任何工作(这对我来说并不多,但很重要).

First and most importantly, make a new branch. This serves two functions, 1) so you shouldn't lose your work if you mess it up (although it still might be a good time to back it up, like to GitHub), and 2) so you can compare the files that have conflicts after this process and retrieve any work that this process overwrites (it wasn't much for me, but it was important).

在终端中,从您要从仅 API 更改为标准的应用程序目录中.运行以下命令进入一个目录,然后让 rails 在现有项目上编写一个新项目.在最初创建应用程序时使用的第二个命令上使用相同的选项.例如,对我来说,我用 -d postgresql --skip-turbolinks --skip-spring -T 替换了下面的 [options] 因为这些是我在创建时使用的选项我的应用程序.我正在使用 --skip-bundle 标志,因为它可能会改变你的 Gemfile 比你想要的更多,而且你可能想在捆绑之前把它改回来.

In the terminal, from the directory of the app you want to change from API only to standard. Run the following commands to go up one directory and then have rails write a new project over your existing one. Use the same options on the second command that you used when creating your app initially. For example, for me I replaced [options] below with -d postgresql --skip-turbolinks --skip-spring -T because those are the options I used when creating my app. I'm using the --skip-bundle flag because it might change your Gemfile more than you want it too and you'll probably want to change some of it back before bundling.

$ cd ..
$ rails new your_app_name --skip-bundle [options] 

现在 rails 将经历为新应用程序创建所有文件的常规过程,但这次它将跳过几乎所有文件,因为它们已经存在.它将在每个存在冲突的地方停止,这就是您需要逐一分析冲突的地方.

Now rails is going to go through it's usual process of creating all the files for a new app, but this time it's going to skip almost all of them because they're already there. It will stop on each one on which there is a conflict, and that's where you'll need to analyze the conflicts one-by-one.

以下是对冲突文件有用的方法:

Here's what worked for me on the conflicted files:

  1. 提交 d 以查看它们之间的差异.
  2. 如果区别只是添加行,则使用 Y 允许它.毕竟,这就是我们这样做的原因.
  3. 如果区别只是删除代码,则使用 n 拒绝它.
  4. 如果区别在于添加和删除代码,请记下该文件以在此过程后返回.然后用 Y 接受它.
  1. Submit d on each one of them to see the differences.
  2. If the difference is only adding lines, then allow it with Y. That's why we're doing this after all.
  3. If the difference is only removing code, then reject it with n.
  4. If the difference is both adding and removing code, write down that file to come back to after this process. Then accept it with Y.

完成后,使用 git 检查您写下的 (4) 中每个文件的差异.您可能希望保留 rails 添加的更改,但随后您可能希望将其删除的所有代码复制回去.这可能包括 Gemfile.

After this is finished, use git to examine the difference on each file from (4) that you wrote down. You'll want to keep the changes that rails added, but then you'll likely want to copy all the code that it removed back in. This will probably include the Gemfile.

一个显着的区别是 Rails 将应用程序控制器从 ActionController::API 的继承改为 ActionController::Base.我想要一个控制器,所以我创建了一个新文件app/controllers/api_controller.rb".然后我将原始 ApplicationController 中的内容复制到新文件中,并将类名更改为 ApiController.然后我将所有现有的 API 控制器更改为从新的 ApiController 继承,而不是从 ApplicationController 继承.

One notable difference is that rails changes the application controller from inheriting from ActionController::API to ActionController::Base. I want one controller for each, so I created a new file `app/controllers/api_controller.rb'. Then I copied what was in my original ApplicationController over to the new file and just changed the class name to ApiController. Then I changed all my existing API controllers to inherit from the new ApiController instead of from ApplicationController.

完成后,然后运行 ​​bundle install 以安装添加到应用程序中的 gems rails.

After that is done, then run bundle install to install the gems rails added into the app.

这对我有用.我希望它有帮助.祝你好运!

That worked for me. I hope it helps. Good luck!

这篇关于如何将 Rails 5 API 应用程序转换为既可以充当 API 又可以充当应用程序的 Rails 应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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