Loading rails env in production - ruby-on-rails

I have deployed a new Rails site to a Linux VM using Capistrano. I am using nginx as the front end and running my Rails app using unicorn.
If I try to run rake routes on the server, I get an error telling me that Rails is not installed, even though Rails is installed. The problem seems to be that the gem search directory is different for the app and the logged in user.
How do I load the Rails environment that my app is seeing as the logged in user?

Just use:
RAILS_ENV=production bundle exec rake routes
The RAILS_ENV part sets your environment variable so your app is loaded in full production mode, including database settings and so on.
The bundle exec part is necessary so that any commands that come after that are executed within the environment of the gems installed in your Gemfile.

Related

Is there a way to connect to a Rails app's console on a linux server?

I have deployed a rails application using capistrano on a linux server and it is running without any problems. When I connect to my remote server via ssh, the folder structure of my app is quite different from what I have on my local machine. I would like to go to project root and say rails console so that I can have access to the console of my application. Is there a way to achieve this?
When I go to ~MyApp/ folder and run rails console it says command rails not found. I think that is probably because the app is running in another folder.
bundle exec to the rescue inside of project folded:
$ RAILS_ENV=production bundle exec rails console
If you've installed with rbenv it could be that you are defaulting to the wrong Ruby version. So you can run rbenv exec to get it to run on the right version, along with bundle exec to run the right version of Rails
So try running this:
rbenv exec bundle exec rails console
To sum up for anyone having the same problem in the future,
I went to MyApp/releases/2020331231231231 which is the latest release of my app and there I used this command RAILS_ENV=production bundle exec rails console and I was able to do whatever I wanted to do in the console.
First you need to move inside the folder where the code is.
Check Capistrano config if deploy_to is defined. (If it's not it should be set to /var/www/#{application}/ by default read here)
Since Capistrano keep older versions of your app go to the current folder which is a symlink of the latest deployed version.
And run ENVIRONMENT=production bundle exec rails c or ENVIRONMENT=production bin/rails c. If doesn't work try with rbenv ENVIRONMENT=production rbenv exec bundle exec rails c

accessing Rails Console after deploy to server

I´m having trouble to access the rails console in production.
I used Capistranoto deploy the app to a VPS
If I cd to deploy#myapp:~/myapp/current$and run bundle exec rails cthere I always get the option list for creating new rails project, like rails new
I've also tried bundle exec rails console --productionand rails consoleetc all with the same outcome.
the thing is I must be able to access the console because I have to create an admin user for active admin
might be worth adding that I'm using Passenger/Capistrano and Nginx on Ubuntu 16
Does anyone know what is going on here? Am I doing something wrong?
*EDIT
After running RAILS_ENV=production bundle exec rails c I get this message
Looks like your app's ./bin/rails is a stub that was generated by Bundler.
In Rails 4, your app's bin/ directory contains executables that are versioned
like any other source code, rather than stubs that are generated on demand.
Here's how to upgrade:
bundle config --delete bin # Turn off Bundler's stub generator
rake rails:update:bin # Use the new Rails 4 executables
git add bin # Add bin/ to source control
You may need to remove bin/ from your .gitignore as well.
When you install a gem whose executable you want to use in your app,
generate it and add it to source control:
bundle binstubs some-gem-name
git add bin/new-executable
Loading production environment (Rails 4.2.5)
irb(main):001:0>
You are missing executable files of bin folder in Production after Capistrano deployment.
You need to remove bin from set :linked_dirs from your Capistrano deploy.rb in order to avoid symlinking it.
You can again try cap production deploy, it would take all the executable files from bin to Production.
Now, you can access the rails console using:
RAILS_ENV=production bundle exec rails c
If you are using Capistrano 3, you can include the rails:console option which will allow you to do this from your local machine to gain access to the console on the remote host:
bundle exec cap production rails:console
https://rubygems.org/gems/capistrano-rails-console
The Rails project is deployed in /deploy/your_project_name/current on the server by default. So, you can access it via SSH or ... and run bundle exec rails c to access the Rails console. It works for me!
check if rvm and regarding gemset is suitable for the app
rvm gemset list
rvm list
and then do
bin/rails rails c -e production

How to run rake in ruby-on-rails application in production?

I am trying to figure out how to deploy my ruby-on-rails application in production.
The operating system is ubuntu and the application uses Postgres. I have managed to deploy the application and am able to login etc... but there are certain functionality that is not working (I am new to the application and rails).
The application uses elastic search, which I have installed and the service is running (I can access the page via http://localhost:9200). But when the application tries to access components of elastic search i get an error.
There is a rake file in the directory /app/lib/tasks, which has several files and one of the them is elasticsearch.rake
namespace :app do
desc "Bootstraping Elasticsearch index"
task(:elasticsearch => :environment) do
Image.__elasticsearch__.create_index! force:true
Image.import
CaseStudy.__elasticsearch__.create_index! force:true
CaseStudy.import
end
end
As the description in the file says this bootstraps the elastic search index. What I don't know is how do I run all this in a production environment? There are other rake files as well and I want to know how to run them.
You should go to the root path of your project directory in production and type the following command:
RAILS_ENV=production bundle exec rake app:elasticsearch
Note:
To check the available list of rake tasks, you can type rake -T
rake task-name RAILS_ENV=production

running Rails console in production

I have just gone live with my first Rails site, but now I have a problem. When I run the project in development mode on my IDE I can run the console to something like:
User.first.name='whatever' to change a users name.
How do I accomplish the same task on a live site in production mode?
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
Pretty easy:
RAILS_ENV=production rails console
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
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.
With Rails 6.1.6 on AlmaLinux8, the below command worked for me.
bundle exec rails console -e production
today with rails 6 run in console RAILS_ENV=production rails console

rails3 app ENV not being recognized as production

I set up my rails app on my linode VPS, phusion passenger is installed and working, so is mysql (I know this cause my friend currently is running 2 production apps on it with the same set up). The VPS is running Ubuntu 10.10 and I'm using apache2 with passenger.
I SFTP'd the app to the server, bundle updated, set up my virtual host and specified RailsEnv to be production and paths are all accurate.
I then restarted the server (as root) with
apachectl -k restart
tried to rake db:migrate and it didn't do anything, so I figured it was because the environment didn't get changed, which is exactly what happend. If I go into the rails console and type Rails.env it gives me development.
I have no idea why, I did everything that should set it to production? Anyone know what I may have missed? Is there somewhere in the app where I'm supposed to change something to say production environment? I thought that only had to be done in rails 2.x
Thanks in advance for any and all help.
The RailsEnv setting is only for Passenger's use. It doesn't affect the commands you type in the shell.
Use
RAILS_ENV=production rake db:migrate
and
RAILS_ENV=production rails console
Or set the RAILS_ENV environment variable in your login shell to production so that you don't have to append RAILS_ENV=production to the commands you issue:
export RAILS_ENV=production
(exact command may vary depending on which shell you use; the above works in bash)
You were on the right track; all you need to do to actually run the app in production mode is set the RailsEnv as you did, assuming you are running the app using Passenger. However, to run the database migrations you need to tell Rails what environment to run within.
The rails console command defaults to the 'development' environment by default. The same goes for running database migrations.
To run the migrations on your production environment you need to run the command as such:
RAILS_ENV=production rake db:migrate
And to run the console in production mode, you need to run the console using the command:
rails console production
If you want this variable to be set automatically, put RAILS_ENV=productionat the end of your ~/.bashrc file. (This only works with a bash terminal)
Then open a new terminal, or restart your ssh connection.

Resources