Rails 5 api only app with homepage

I created a rails application 5 api. But I want my application to be on the home page. To do this, I generated a home controller and added a file of the form I corresponding views /home/index.html.erb

But when I tried to access it, I get below response

Start GET "/ home / index" for 127.0.0.1 at 2016-07-14 11:14:03 +0530 Indexed by HomeController # as HTML Completed 204 No content at 0ms

GET "/home/index.js" will start for 127.0.0.1 at 2016-07-14 11:14:20 +0530 Processing of HomeController # index as JS Completed 204 No content at 0ms

But I could not see the contents of the index page displayed on the Internet.

Share your thoughts.

+4
source share
2 answers

I was in the same boat trying to make a Rails 5 API application that could still load from one html page (taken by JS at boot). Having stolen a hint from the rails source , I created the following controller (note that it uses Rails instead of mine ApplicationControllerfor this single non-api controller)

require 'rails/application_controller'

class StaticController < Rails::ApplicationController
  def index
    render file: Rails.root.join('public', 'index.html')
  end
end

and put the appropriate static file (plain .html, not .html.erb) in the folder public. I also added

get '*other', to: 'static#index'

at the end routes.rb(after all my api routes) to ensure that client-side routing is maintained for reloads, deep links, etc.

routes.rb, Rails / non-api. public/index.html ( root routes.rb) , StaticController

get '*other', to: redirect('/')

, .

, - .

+11

HTML ( css/js), , - index.html public.

+1

All Articles