简体   繁体   中英

Rails heroku deploy jquery and css errors, set amazon s3 variables locally

When I push to heroku the logs don't report errors and everything seems fine. But bits and pieces of the css and jquery on the online production site are faulty. So I started my local server with rails server -e production and immediately got the error Missing required arguments: aws_access_key_id, aws_secret_access_key . How do I set these locally without hard-coding? And what's the likely reason for production css/javascript failing when there are no reported errors?

to set environment variables locally install the gem

# Gemfile
gem 'dotenv-rails'

Then create a file called

.env 

and place it in the same folder/location as your .gitignore file (ie top of your app folder).

Inside .env file put in your env variables like this

aws_access_key_id=1234556
aws_secret_access_key=1234556

Now don't forget to add the .env file to your .gitignore file like so ...

#.gitignore
# Ignore .env (local development environment vairables)
.env

Rails 4.1+ has a set of "secret" keys you can assign in config/secrets.yml :

Rails 4.1 generates a new secrets.yml file in the config folder. By default, this file contains the application's secret_key_base, but it could also be used to store other secrets such as access keys for external APIs.

This is used to give you a series of values in the Rails.application.secrets hash, which is environment dependent.

Thus, what you should have done is as follows:

#config/secrets.yml
development:
  aws_secret_key:    "x"
  aws_access_key_id: "y"
production:
  aws_secret_key:    ENV["AWS_SECRET"]
  aws_access_key_id: ENV["AWS_ACCESS"]

The reason you have the error is because you're invoking rails server for the production environment. Since you're using the forementioned keys in production, their absence is going to cause problems when running on your development box.

The best way to resolve this issue is to use Rails.application.secrets.aws_secret_key wherever your AWS config is set (presumably config/environments/production.rb ).

Whilst it won't solve the problem directly, it will help you create a seamless transition between development & production (which should be as similar as possible).


To solve the problem itself, you should set the ENV vars for aws_secret_key and aws_access_key_id on your local machine.

This is done at OS level - in Windows (which is what we use), you can use the following .

Make sure you have the ENV vars set on your system, and the app should run in production mode.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM