How do I run and reflect rails associates:precompile while rails are running?

Asked 2 years ago, Updated 2 years ago, 43 views

We are thinking of running the rails associates:precompile while the rails are running and reflecting it as it is without restarting.

[Final goal]

  • Registered users have their own profile page.
  • The profile page has a separate CSS for each user.
  • User can change CSS parameters
  • After the user changes the CSS, run assembets:precompile in the background.
  • assets: When precompile is complete, it will appear on the profile page.

とCurrent situation and problems 点
The CSS change acceptance of the user and the reflection of it in the scss file have been completed.However, the reflection is not immediately reflected when you run rakeassets:precompile.Once the server (rails) is stopped and restarted ($rails)

We would like the CSS changes to be reflected without restarting the server.
Could you tell me how to implement this?

内容Current status br
To check in the local environment.Rewrite the development configuration to the same as production.

#config/environments/development.rb
Rails.application.configured do
  config.cache_classes=true

  config.eager_load = true

  config.consider_all_requests_local=true
  config.action_controller.perform_caching=true

  config.serve_static_files=ENV['RAILS_SERVE_STATIC_FILES'].present?

  config.assets.js_compressor= —uglifer

  config.assets.compile=true

  config.assets.digest=true

  config.log_level=:debug

  config.i18n.fallbacks = true

  config.active_support.deprecation=:notify

  config.log_formatter=::Logger::Formatter.new
  config.active_record.dump_schema_after_migration=false

end

Launch Rails Server

$rails

Access the user's profile page.The log reads the current css.

INFO--:Started GET"/assets/application-8bbf8d83f787bfc34b831bf49ce85bae1fc1ac7d8589f27607c326df773d8a4f.cs" for::1

Change the CSS file by entering it from the CSS edit page.
App/assets/stylesheets/ has a per-user scss file that is updated as input.
Example: scss file for users with id:3
User 3 input =>color:red,size:44

#app/assets/stylesheets/3_user.scss
h1.user_3{ 
  color:red;
  font-size:44px;
}

rakeassets:precompile updates the fingerprint.

$bundle exec rake assemblies:precompileRAILS_ENV=development
INFO--: Writing/public/assets/application-140f7622546843fcda02f1312e5cd500bcb5ae16f3688b14cdb86e91432f9a68.css

Reloading the browser to view the same user profile and looking at the log shows exactly the same fingerprint as before the precompilation.The CSS on the page also does not reflect any updates.

INFO--:Started GET"/assets/application-8bbf8d83f787bfc34b831bf49ce85bae1fc1ac7d8589f27607c326df773d8a4f.cs" for::1

Restarting the server, accessing the same page and checking the log switches the fingerprint to a new one, and updating the page content that the browser sees.

INFO--:Started GET"/assets/application-140f7622546843fcda02f1312e5cd500bcb5ae16f3688b14cdb86e91432f9a68.cs" for::1

Why do I need to reboot the server to reflect the updates?If I check the local folder (public/assets/) and it definitely has a file with a new fingerprint, why isn't it reflected in my web browser when it's still running?

ruby-on-rails ruby

2022-09-30 16:28

1 Answers

> Why do I need to reboot the server to reflect the updates?
Perhaps the variable contains the CSS name that layout/application.* should return when starting the server.If you know the variable, you can change it.

Save the edited CSS to public/somewhere with a user-specific name and
The code to load it into layout/application.* is
 <link rel="stylesheet" href="/somewhere/<%=current_user.css_filename%>"/>


2022-09-30 16:28

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.