在生产中运行 Rails 控制台

我刚刚使用了我的第一个 Rails 站点,但是现在我遇到了一个问题。当我在 IDE 上以开发模式运行项目时,我可以将控制台运行到类似于:

User.first.name='whatever'更改用户名。

在生产模式下,我如何在现场完成同样的任务?

90151 次浏览

Note: This answer assumes you are using Heroku as your hosting service.

It depends on what hosting service you are using. For Heroku, you can go to your terminal and type in

heroku run rails console

This will load up the rails console for your production site and will allow you to create records for your live site.

You can also look into seeding a database but that is generally meant for testing. RailsCasts has some videos on the topic but they are a bit outdated.

Pretty easy:

RAILS_ENV=production rails console

if you're running rails 3.0 or greater, you can also use

rails console production

production can of course be substituted with development or test (value is development by default)

Adding the option --sandbox makes it so that any changes you make to your database in the console will be rolled back after you exit

If this isn't working for you, you may need to try

bundle exec rails console production

If you are actually trying to run the rails console on your production server, try googling "run rails console [your cloud hosting provider]" e.g. "run rails console heroku"

As of Rails 6 you need to use

RAILS_ENV=production bundle exec rails c

or

RAILS_ENV=production rails c

depending on your setup

If you have already deployed your site to the server, you can also use:

bundle exec rails console production

...in the webroot of your rails app. That is if you haven't installed the rails package directly on the server yet or if you want to run console within the context of your web app.

Try below command.

  rails c -e production

today with rails 6 run in console RAILS_ENV=production rails console

With Rails 6.1.6 on AlmaLinux8, the below command worked for me.

bundle exec rails console -e production