Sahat Yalkabov
Sahat Yalkabov

Reputation: 33664

How to override public folder path in Rails 4?

I would like to use a different public folder from a parent directory called client which contains the entire AngularJS app. Essentially I want to tell Rails to load AngularJS app and the only job that Rails has to do is serve JSON.

Is that possible in Ruby on Rails?

Upvotes: 2

Views: 3790

Answers (3)

GS Shahid
GS Shahid

Reputation: 39

You can define another path like

# config/application.rb
paths['my_website'] = 'website'

Then you can use this path in your routes like

# routes.rb
get '/my_website', to: redirect('my_website/index.html')

Upvotes: -1

earksiinni
earksiinni

Reputation: 407

As others have mentioned, it may or may not be a great idea to override the existing paths['public'] folder. But you can do the following safely in somewhere like application.rb:

Rails.application.config.middleware.insert_after(
  ActionDispatch::Static,
  ActionDispatch::Static,
  Rails.root.join("client").to_s,
  Rails.application.config.static_cache_control
)

The public folder is exposed to the web server through the Rack middleware ActionDispatch::Static. There's nothing else special about it, and the above code simply adds another instance of the middleware that points to the directory client. So in the above case, the browser would be able to access everything in public as well as client.

Upvotes: 9

Vigintas Labakojis
Vigintas Labakojis

Reputation: 1069

Just had to do it myself for an Angular app.

Put this in your application.rb:

config.serve_static_files = true
paths['public'] = File.join 'client', 'app'

Or if you still use asset pipeline (config.assets.enabled = true):

paths['public/javascripts'] = File.join 'client', 'app', 'scripts'
paths['public/stylesheets'] = File.join 'client', 'app', 'styles'

Would be interesting to know if there are any consequences with the second bit as my front-end is served completely separately thus I keep asset pipeline switched off and use grunt instead.

Upvotes: 1

Related Questions