Webpacker: Rails API + Webpacker?

Created on 17 Mar 2018  路  2Comments  路  Source: rails/webpacker

Is is possible to use with rails new app --api --webpack ?

Naturally it would be handled differently in production, but I thought it would be a quick way to host & manage an SPA side by side with a rails api in development mode.

Since there's no views, I can't use <%= javascript_pack_tag '...' %>

Most helpful comment

thanks @danielpowell4 馃挴

The other option is to use something like html webpack plugin, where you don't need views at all: made a quick example here: https://github.com/gauravtiwari/webpacker-api-frontend

You can use a client side router to route appropriately.

@foucist Does this answers your question?

All 2 comments

@foucist Flagging your app creation with an --api flag means there are no client view files

As such it wouldn't make sense to include webpacker, which only exists for that "missing" frontend

You could use Rails as an api backend without removing the views. Adding a constraint like this in your routes file could very well make sense.

namespace :api, constraints: { format: 'json' } do
  resources :foo, only: :index
  resources :bar, only: :create
end

This would create the following routes:

GET api/foos      at Api::FoosController#index         in app/controller/api/foos_controller.rb
POST api/bars     at Api::BarsController#create        in app/controller/api/bars_controller.rb

You'd then build an SPA and serve it up at the root

Otherwise, your best to get moving quickly is probably to use a client-centric boilerplate like create react app. This will likely include keeping track of two repos, a versioned API, and CORS requests

thanks @danielpowell4 馃挴

The other option is to use something like html webpack plugin, where you don't need views at all: made a quick example here: https://github.com/gauravtiwari/webpacker-api-frontend

You can use a client side router to route appropriately.

@foucist Does this answers your question?

Was this page helpful?
0 / 5 - 0 ratings