简体   繁体   中英

How do you convert a Rails 5 API app to a rails app that can act as both API and app?

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

From http://edgeguides.rubyonrails.org/api_app.html ,

I removed config.api_only = true

I changed

class ApplicationController < ActionController::API
end

to

class ApplicationController < ActionController::Base
end

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.

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

Thanks!

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.

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.

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

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] 

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

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.

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.

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!

From a directory outside of the api application (such as its parent - cd .. ) I would do

rails new comparison_real_app

and then compare the contents of the comparison_real_app with your app and copy over the files that are missing into the api app and change any other files as required.

因此,我可能需要做更多的事情,但要解决样式表的问题,您需要手动创建views/layouts/application.html.erbassets/stylesheets/application.css文件。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM