Ruby on rails : launch server with code reload - ruby-on-rails

I'm using a server for testing my api. I use this command to launch the server :
rails s -p 3001 -e test -P 42342
I change the port to 3001, to not have a conflict with my dev server (on port 3000).
The thing is that when I change code, the server don't reload the code, I have to kill the server and relaunch.
Is there a parameters that I miss to launch my rails command ?

test is only designed for automated testing, with Rails running for a single test run. Since code should not be changing during a test case, it doesnt have many of the development mode features and is more like production in that regard. Its also intended to reset your database when running tests, which also doesnt seem to be your intention.
If you want (most) classes to auto-reload, always use the development environment.
If for some reason you need a different environment (more than just port, but different configs), you can look at creating a new environment, copying the configs for development (add new entries to config/environments/, config/database.yml, config/secrets.yml, and use as a group in the Gemfile.).

Related

Is one rails server per application?

I have two questions about rails server:
Do I have to start the server from within the application folder?
Is the server I started only for that application?
If they are true, this does not quite make sense to me, since why do I need to start multiple servers?
Or is there some kind of master configuration, so that one server can route to different applications? Is Capistrano for this purpose?
I'm going to assume you're talking about the rails server command, for running a local rails server to test your application, and that you're not talking about setting up a rails application on a remote server. Please mention if that is not the case.
Yes, you must execute rails server from within the root folder of your rails application.
Yes, the server you started is only for that application. It's a self-contained thing.
You should not need to start multiple servers. Even if you have multiple applications, you probably don't need to have more than one running at a time. So, you can shut down the rails server in one application (Ctrl-C) and then cd to your new application, and start a new rails server there with rails server.
If you do need to run two local rails applications at once, you can do so by running them on different ports. So, the first one, you can just execute rails server and it will make your site available at localhost:3000 (because port 3000 is the default port). The next one, you can specify a port other than 3000 - eg. rails server -p 3001 to get a rails app at localhost:3001.
Capistrano is for deploying your applications to a remote server, not for running them locally on your own computer. So, it is not relevant here. What you may be interested in is http://pow.cx/
Again, I've assumed you're talking about running your rails app locally on your own computer. If you're referring to deploying it to the internet on a server, then you can ignore this answer.

rails scope root and multiple apps

I have a working rails app accessible directly from http://0.0.0.0:3000/ . The app is also in staging and production in heroku.
Today, I want to start working on a new rails app in the same computer. How can I start working on the new unrelated app under a different path without messing up my staging/production urls ?
How can I have something like this locally and switch between the two apps
http://0.0.0.0:3000/existingApp/
http://0.0.0.0:3000/newapp/
I tried scope "/existingApp" do in my routes.rb for / and I suppose I should do that for the new app as well... but how do I specify this only for my local environment? I would like my heroku urls to stay unchanged (ie stay at the root).
I wouldn't recommend doing what you're doing, but if you're gonna do it anyway, you could try writing an engine and mounting it.
This could help get you started.
Almost any server would be able to be configured to listen to a port other than 3000. Thin, for example, can be started as:
thin -R config.ru -a 127.0.0.1 -p 8080 start
And would then listen on port 8080. Rails server can be started similarly using:
rails server -e production -p 4000
You may want to also consider starting your database using a port different than the standard one, but that is probably not necessary.

Rails 4 Change port number only for production environment

I found this question before:
How to change Rails 3 server default port in develoment?
However what I really want is to change my port number for the production environment only. I am using RoR 4. It would be very nice if I could type something on production.rbin config/environments. Is there a way to do that?
The answer in brief
The rails stack has your application and then the server that runs your application (aka the "application server"). This server could be webrick (not a good idea in production), thin, gunicorn, passenger etc etc.
You should be telling that server which port to run under. You'll (likely) need to specify this outside Rails - not in config/production.rb because by the time Rails boots it's already running inside some application server.
A deeper dive with an example:
Let's use Heroku for an example, because port numbers there are essentially randomized (at least from the view of us looking in).
Heroku will pick a random port for us, then tell us through the PORT environmental variable. With Heroku you need a Procfile to tell it what services to launch, and your Procfile may look something like this:
web: bundle exec unicorn -p $PORT -c ./config/unicorn.rb
See here, we use -p $PORT to tell unicorn - our application server in this example - to run on some port Heroku gave us.
Back to your question:
However you kick off your application serving process in your production environment, you should tell it to specify the port number to your web server. There's a bunch of ways to kick off your application serving process at a production level: from upstart (built into Ubuntu), to supervisord to god... all these methods run commands and make sure the process stays up (an important part of production level deployment ;) )

Should the database.yml be configured when testing locally for Heroku with foreman?

Can someone explain to me what they do when they initialise a rails app locally with foreman (part of Heroku toolbelt) (using postgreSQL), destined to run on Heroku?
I'm going by this guide: developing locally with foreman and what I don't understand is if we are expected to specify database username and passwords or if foreman is supposed to handle it as Heroku itself does?
This perplexes me a little as if we are supposed to modify the database.yml to hook it up to postgreSQL, then what is the point of using foreman instead of rails server?
If it does handle it, how does it handle it, and how would I configure my pg_hba.conf to respect it? Something like local all myuser trust?
Yes, database.yml needs to be configured with valid information for your development and test databases.
Foreman is only running what's in your Procfile, not ripping things out and plugging different things in like Heroku does.
So why do you want to use Foreman instead of rails server? Because it:
Runs all roles defined in your Procfile with one command
Automatically loads your .env
Will fail if any of your roles fail (so less scratching your head because some necessary backend service isn't running)

How do I work on my Rails app in development mode, but push it in production mode?

Simple question, I do all my work on my local at home computer and then I git push to my repo on github, and then on my Ubuntu Server I do a git pull to get the content, but this causes inconsistencies because it pulls in development mode which I always have to go in and change. Is there a way to make it so I can work in development mode but push it to the repo production mode? Thanks.
This isn't a git problem. Rails environments ('production', 'development', 'test') affect how the code runs, but the code isn't changed.
When you run rails server on your development machine, the application starts in development mode because that is the default for that command.
You probably aren't using the same command to host your site on WEBrick on your server, but rather using something like Phusion Passenger, which can (should) be configured to boot your application in Production mode.
If the environment is set correctly, the config/environments/#[RAILS_ENV}.rb file is correctly selected upon loading the app, and the correct database sources are selected from config/database.yml.
Rails apps default to development unless you set the RAILS_ENV variable to production. Git doesn't really factor in to this. On your production machine, you'll want to set RAILS_ENV.
Where to do this depends on your production deployment environment, and how your server is configured. Things like Passenger default to production mode, and configuring that depends on your server (Apache, nginx). If you're manually starting the server from the command line (via rails s thin or something along those lines), then you can configure it in your shell's startup file (.profile or something similar, depending on your shell). thin also optionally takes an environment argument like -e production.
If you provide more details on how your production environment works, you might get a more specific answer for where to set RAILS_ENV.

Resources