Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 320
Laser Avatar asked Apr 16 '16 21:04

Laser


People also ask

What is a Rails API?

What is an API Application? Using Rails as an API means a user is provided a web application and also an accessible API. An Application Programming Interface (API) is a computing interface that defines interactions between multiple software intermediaries.

Can you make apps with Ruby on Rails?

Absolutly YES! Ruby on Rails just a backend tech, is the same as what you plan to do for normal browser. There are only two things that you need to consider about mobile app.


Video Answer


3 Answers

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!

like image 80
Calaway Avatar answered Oct 11 '22 00:10

Calaway


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.

like image 41
Michael Durrant Avatar answered Oct 11 '22 01:10

Michael Durrant


So there's probably more things that will need to be done as I (you) go along, but to resolve the issue with stylesheets you need to manually create your views/layouts/application.html.erb and assets/stylesheets/application.css files.

like image 1
Laser Avatar answered Oct 11 '22 01:10

Laser