Home > Net >  Rails recompile assets with data from database
Rails recompile assets with data from database

Time:12-22

I want to recompile assets with data from database. My app will only have less than 10 users and I want to compile stylesheets in public/assets folder at the time of deployment.

In database, I have a table that stores user's color preference.

      user_id | primary_bkg | secondary_bkg
        1234      red            blue
        1235     yellow         blue

config/environments/development.rb

config.assets.compile = true

The variables from user's color preference table will override the ones in application.css

Where can I trigger recompile assets and pull data from database?

CodePudding user response:

If you want to precompile at the time of deployment, how to achieve it depends on your chosen framework of deployment. In general, configure your deployment framework so that it runs (providing it is Rails 5 )

RAILS_ENV=production rails assets:precompile

at the time of deployment (or run it manually at the time of every deployment).

Alternatively, you can write

config.assets.compile = true

in your config/environments/production.rb (not development.rb), although that means every access will prompt recompile of the assets and so is resource-heavy.

CodePudding user response:

Static assets are not dynamically created. They are just plain CSS or plain JS.

If I understand well you want as many CSS files as you have users. It wil create so many more static assets than needed. It does not look very efficient.

Why don't you try to compile your assets the normal way, have these static assets being common to every user and add the background color dynamically set from the html :

<div style="background-color: <%= @user.primary_bkg %>;">
</div>
  • Related