Rails 5 api only app with home page
Asked Answered
A

3

10

I have generated a rails 5 api application. But I want my app with home page. For that I generated a home controller and added view file i respective views/home/index.html.erb

But when I tried accessing it I am getting below response

Started GET "/home/index" for 127.0.0.1 at 2016-07-14 11:14:03 +0530 Processing by HomeController#index as HTML Completed 204 No Content in 0ms

Started GET "/home/index.js" for 127.0.0.1 at 2016-07-14 11:14:20 +0530 Processing by HomeController#index as JS Completed 204 No Content in 0ms

But I could not see index page content displayed on the web.

Kindly share your thoughts.

Ahwaz answered 14/7, 2016 at 5:57 Comment(0)
T
22

I was in the same boat, trying to do a Rails 5 API app that could still bootstrap from a single html page (taken over by JS on load). Stealing a hint from rails source, I created the following controller (note that it's using Rails' instead of my ApplicationController for this lone 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 corresponding static file (plain .html, not .html.erb) in the public folder. I also added

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

at the end of routes.rb (after all my api routes) to enable preservation of client-side routing for reloads, deep links, etc.

Without setting root in routes.rb, Rails will serve directly from public on calls to / and will hit the static controller on non-api routes otherwise. Depending on your use-case, adding public/index.html (without root in routes.rb) might be enough, or you can achieve a similar thing without the odd StaticController by using

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

instead, if you don't care about path preservation.

I'd love to know if anyone else has better suggestions though.

Thirsty answered 16/7, 2016 at 0:13 Comment(3)
Nice! I had the same problem and you helped me. But you don´t need "layout false" line.Hagan
This is the right answer and allows client-side routing solution (i.e. React Router) to pick up the route and render the right thing. What I do to have my controllers all "look" the same is create an ApiController and an ApplicationController inheriting from ActionController::API and ActionController::Base, respectively. Then inherit from ApplicationController for StaticController and all others can inherit from ApiController.Leflore
This didn't work in my Rails 7 app since I didn't have the file renderer. My workaround was to pretend I was serving XML and override the content type: render xml: File.read(Rails.root.join('public', 'index.html')), content_type: 'text/html'Attemper
E
0

Just to add to Risto Keravuori's answer

I had to create a home controller in the app/controllers directory:

app/controllers/home_controller.rb

Next, I added the following content to it:

require 'rails/application_controller'

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

Then, I created an index.html file in the public directory:

public/index.html

Next, I added the following content to it:

<head>
  <title>My API</title>
</head>
<body>
    <h1>My API</h1>
    <p>Welcome to my API</p>
</body>

Note: You can modify the index.html file based on your choice.

Lastly, I added the routes to the config/routes.rb file, below all the already defined routes.

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

Note: Do not put it inside a namespace say, api namespace or api/v1 namespace. Just put it at the bottom of the file but inside the Rails.application.routes.draw do block.

You can restart your server and test it out.

That's all.

I hope this helps

Ebert answered 17/12, 2020 at 14:37 Comment(0)
L
-1

If all you need is just a static HTML page (with some simple css/js), i think another method is just to edit the index.html page under public folder.

Liggitt answered 9/7, 2017 at 5:27 Comment(2)
this is correct just create an index.html file inside the public folder not need to make a new routes/controllerForeshorten
This is Rail API controller, it is not serve static public pagesChukar

© 2022 - 2024 — McMap. All rights reserved.