Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rails API + Webpacker? #1361

Closed
foucist opened this issue Mar 17, 2018 · 2 comments
Closed

Rails API + Webpacker? #1361

foucist opened this issue Mar 17, 2018 · 2 comments

Comments

@foucist
Copy link

@foucist foucist commented Mar 17, 2018

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

@danielpowell4

This comment has been minimized.

Copy link

@danielpowell4 danielpowell4 commented Mar 19, 2018

@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

@gauravtiwari

This comment has been minimized.

Copy link
Member

@gauravtiwari gauravtiwari commented Mar 20, 2018

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants
You can’t perform that action at this time.