<commit_before>---
title: Getting Started Deploying Ruby on Rails Apps
---

<%=vars.GSG_intro_sentence%>

This guide is intended to walk you through deploying a Ruby on Rails (RoR) app to <%=vars.product_full%>. You can choose whether to push a sample app, your own app, or both.

If at any time you experience a problem following the steps below, try checking the <%=vars.known_issues%> topic, or refer to the [Troubleshooting Application Deployment and Health](./../../devguide/deploy-apps/troubleshoot-app-health.html) topic for more tips.

## <a id='deploy-sample'></a>Deploy a RoR Sample Application ##

This section describes how to deploy a RoR sample application named `rails_sample_app`.

To learn how to deploy your own RoR app, see [Deploy Your RoR Application](#deploy-yourapp).

### Prerequisites ###

* [Git](http://git-scm.com) configured on your workstation and a [GitHub](https://github.com) account
* Basic RoR knowledge

### Step 1: Get a Sample RoR App from GitHub ###

Run `git clone https://github.com/cloudfoundry-samples/rails_sample_app` to clone `rails_sample_app` from GitHub.

### Step 2: Log in and Target the API Endpoint   ###
Run `cf login -a [API_ENDPOINT]`, enter your login credentials, and select a space and org. The API endpoint is <%=vars.api_endpoint%>.

### Step 3: Deploy the Sample App ###

1. `cd` to the cloned app directory.
1. Run `cf create-service elephantsql turtle rails-postgres`.
1. Run `cf push rails_sample_app -n [HOST_NAME]`.

Example: `cf push rails_sample_app -n jc0825-rails`

  `cf push [APP_NAME]` pushes the specified app from the current directory to <%=vars.product_short%>. Use the `-n` flag to create a unique host name for the app to avoid URL collisions with other apps.

  `cf push` automatically searches for an [application manifest YAML file](./../../devguide/deploy-apps/manifest.html) (`manifest.yml`) and also determines the appropriate [buildpack](./index.html) for the app.  A manifest specifies what `cf push` should do with the app and the buildpack provides framework and runtime support for your app.

  The example shows the terminal output of deploying the `rails_sample_app`. `cf push` uses the instructions in the manifest file to create the app, create and bind and the route, and upload the app to <%=vars.product_short%>. It then binds the app to the `rails-postgres` service you defined in the previous step, and follows the instructions in the manifest to start one instance of the app with 256M. After the app starts, the output displays the health and status of the app.

<pre class="terminal">

$ cf push rails_sample_app -n jmt2014-rails
Using manifest file /Users/pivotal/workspace/rails_sample_app/manifest.yml

Updating app rails_sample_app in org Cloud-Apps / space development as <EMAIL>...
OK

Using route jmt2014-rails.<%=vars.app_domain%>
Uploading rails_sample_app...
Uploading app files from: /Users/pivotal/workspace/rails_sample_app
Uploading 445.7K, 217 files
OK
Binding service rails-postgres to app rails_sample_app in org Cloud-Apps / space development as <EMAIL>...
OK

Starting app rails_sample_app in org Cloud-Apps / space development as <EMAIL>...
OK

...

0 of 1 instances running, 1 starting
1 of 1 instances running

App started

Showing health and status for app rails_sample_app in org Cloud-Apps / space development as <EMAIL>...
OK

requested state: started
instances: 1/1
usage: 256M x 1 instances
urls: jmt2014-rails.<%=vars.app_domain%>

     state     since                    cpu    memory          disk
#0   running   2014-08-25 03:32:10 PM   0.0%   68.4M of 256M   73.4M of 1G

</pre>

### Step 4: Test the Sample App ###

You’ve deployed your first app to <%=vars.product_short%>! Now let’s access your app online using its URL.
In the terminal output, the `urls` field of the `App started` block contains the app URL. This is the `HOST_NAME` you specified with the `-n` flag plus the domain `<%=vars.app_domain%>`. The image shows the resulting webpage for the `rails_sample_app`.

<%= image_tag("../images/RoR_sample_webpage.png") %>

You can now use the `cf` <%=vars.dev_console_2%> to review information and administer your app and your <%=vars.product_short%> account. For example, you could edit the `rails_sample_app/manifest.yml` to increase the number of app instances from 1 to 3, and redeploy the app with a new app name and host name.

See the [Manage Your Application with the cf CLI](#cli-manage) section for more information. <%=vars.dev_console_4%>

## <a id='deploy-yourapp'></a>Deploy Your RoR Application ##

This section describes how to deploy your RoR application to <%=vars.product_short%>.

### Prerequisites ###

* [Bundler](http://bundler.io) configured on your workstation
- (**Optional**) [Git](http://git-scm.com) configured on your workstation and a [GitHub](https://github.com) account
* Intermediate to advanced RoR knowledge
- (**Optional**) Robust production web server for Ruby and RoR apps, such as Unicorn
* Your Rails 4.x app that runs locally on your workstation

### Step 1: Add the rails_12factor Gem to Your Gemfile ###

1. Add `gem rails_12factor group: :production` to your Gemfile.

    <p class="note"><strong>Note</strong>:  This gem includes the <code>rails&#095;serve&#095;static&#095;assets</code> gem that enables your Rails app to run in production with the static assets serving functionality. The <code>rails&#095;serve&#095;static&#095;assets</code> gem does this by automatically modifying the <code>config.serve&#095;static&#095;assets</code> configuration parameter from <code>false</code> to <code>true</code>. Do not change the value for this option. For more information, refer to the <a href="https://github.com/heroku/rails_12factor">Rails_12factor GitHub README</a>.</p>

1. Run `bundle install` to update your Gemfile.

### Step 2: (Optional) Configure Automatically Invoked Rake Tasks for Deployed Applications ###

<%=vars.product_short%> does not provide a way to run a Rake task on a deployed application. For <%=vars.product_short%> to automatically invoke a Rake task while an app is deployed, you must include the Rake task in your application and also configure the application start command with the `command` attribute in the application manifest, `manifest.yml`.
The following is an example of how to invoke a Rake database migration task at application startup.

1. Create a file with the Rake task name and the extension `.rake`, and store it in the `lib/tasks` directory of your application.

1. Add the following code to your rake file:

    <pre class="code">
    namespace :cf do
        desc "Only run on the first application instance"
        task :on_first_instance do
            instance_index = JSON.parse(ENV["VCAP_APPLICATION"])["instance_index"] rescue nil
        exit(0) unless instance_index == 0
        end
    end

    </pre>

    This Rake task limits an idempotent command to the first instance of a deployed application.

1. Add the task to the `manifest.yml` file with the `command` attribute, referencing the idempotent command `rake db:migrate` chained with a start command.

    <pre class="code">
applications:
 &#8208; name: my-rails-app
 command: bundle exec rake cf:on_first_instance db:migrate && rails s -p $PORT

    </pre>


For information on how to configure a manifest file, refer to [Step 4: Configure the Manifest File](#yourapp-step4). 

### <a id='yourapp-step3'></a>Step 3: Create and Bind a Service Instance for Your RoR Application ###

This section describes using the CLI to configure a PostgreSQL managed service instance for your app. <%=vars.dev_console_1%>

<%=vars.product_short%> supports two types of service instances:

* Managed services integrate with <%=vars.product_short%> through service brokers that offer services and plans and manage the service calls between <%=vars.product_short%> and a service provider. 
* User-provided service instances enable you to connect your application to pre-provisioned external service instances.

For more information on creating and using service instances, refer to the [Services Overview](./../../devguide/services/index.html) topic.

#### Create a Service Instance ####

Run `cf marketplace` to view managed and user-provided services and plans available to you. 

* Managed services integrate with <%=vars.product_short%> through service brokers that offer services and plans and manage the service calls between <%=vars.product_short%> and a service provider.
* User-provided service instances enable you to bind your application to pre-provisioned external service instances.

For more information on creating and using service instances, refer to the [Services Overview](./../../devguide/services/index.html) topic.

Run `cf marketplace` to view managed and user-provided services and plans available to you.

The example shows three of the available managed database-as-a-service providers and their offered plans: `cleardb` MySQL, `elephantsql` PostgreSQL as a Service, and `mongolab` MongoDB-as-a-Service.

<pre class="terminal">

$  cf marketplace
Getting services from marketplace in org Cloud-Apps / space development as <EMAIL>...
OK


service          plans 	description

...
cleardb          spark, boost, amp, shock  	Highly available MySQL for your Apps

...
elephantsql      turtle, panda, hippo, elephant   PostgreSQL as a Service
...
mongolab         sandbox   	Fully-managed MongoDB-as-a-Service
...
</pre>
<commit_msg>delete duped 'run cf marketplace...' sentence in gsg-ror.html<commit_after>---
title: Getting Started Deploying Ruby on Rails Apps
---

<%=vars.GSG_intro_sentence%>

This guide is intended to walk you through deploying a Ruby on Rails (RoR) app to <%=vars.product_full%>. You can choose whether to push a sample app, your own app, or both.

If at any time you experience a problem following the steps below, try checking the <%=vars.known_issues%> topic, or refer to the [Troubleshooting Application Deployment and Health](./../../devguide/deploy-apps/troubleshoot-app-health.html) topic for more tips.

## <a id='deploy-sample'></a>Deploy a RoR Sample Application ##

This section describes how to deploy a RoR sample application named `rails_sample_app`.

To learn how to deploy your own RoR app, see [Deploy Your RoR Application](#deploy-yourapp).

### Prerequisites ###

* [Git](http://git-scm.com) configured on your workstation and a [GitHub](https://github.com) account
* Basic RoR knowledge

### Step 1: Get a Sample RoR App from GitHub ###

Run `git clone https://github.com/cloudfoundry-samples/rails_sample_app` to clone `rails_sample_app` from GitHub.

### Step 2: Log in and Target the API Endpoint   ###
Run `cf login -a [API_ENDPOINT]`, enter your login credentials, and select a space and org. The API endpoint is <%=vars.api_endpoint%>.

### Step 3: Deploy the Sample App ###

1. `cd` to the cloned app directory.
1. Run `cf create-service elephantsql turtle rails-postgres`.
1. Run `cf push rails_sample_app -n [HOST_NAME]`.

Example: `cf push rails_sample_app -n jc0825-rails`

  `cf push [APP_NAME]` pushes the specified app from the current directory to <%=vars.product_short%>. Use the `-n` flag to create a unique host name for the app to avoid URL collisions with other apps.

  `cf push` automatically searches for an [application manifest YAML file](./../../devguide/deploy-apps/manifest.html) (`manifest.yml`) and also determines the appropriate [buildpack](./index.html) for the app.  A manifest specifies what `cf push` should do with the app and the buildpack provides framework and runtime support for your app.

  The example shows the terminal output of deploying the `rails_sample_app`. `cf push` uses the instructions in the manifest file to create the app, create and bind and the route, and upload the app to <%=vars.product_short%>. It then binds the app to the `rails-postgres` service you defined in the previous step, and follows the instructions in the manifest to start one instance of the app with 256M. After the app starts, the output displays the health and status of the app.

<pre class="terminal">

$ cf push rails_sample_app -n jmt2014-rails
Using manifest file /Users/pivotal/workspace/rails_sample_app/manifest.yml

Updating app rails_sample_app in org Cloud-Apps / space development as <EMAIL>...
OK

Using route jmt2014-rails.<%=vars.app_domain%>
Uploading rails_sample_app...
Uploading app files from: /Users/pivotal/workspace/rails_sample_app
Uploading 445.7K, 217 files
OK
Binding service rails-postgres to app rails_sample_app in org Cloud-Apps / space development as <EMAIL>...
OK

Starting app rails_sample_app in org Cloud-Apps / space development as <EMAIL>...
OK

...

0 of 1 instances running, 1 starting
1 of 1 instances running

App started

Showing health and status for app rails_sample_app in org Cloud-Apps / space development as <EMAIL>...
OK

requested state: started
instances: 1/1
usage: 256M x 1 instances
urls: jmt2014-rails.<%=vars.app_domain%>

     state     since                    cpu    memory          disk
#0   running   2014-08-25 03:32:10 PM   0.0%   68.4M of 256M   73.4M of 1G

</pre>

### Step 4: Test the Sample App ###

You’ve deployed your first app to <%=vars.product_short%>! Now let’s access your app online using its URL.
In the terminal output, the `urls` field of the `App started` block contains the app URL. This is the `HOST_NAME` you specified with the `-n` flag plus the domain `<%=vars.app_domain%>`. The image shows the resulting webpage for the `rails_sample_app`.

<%= image_tag("../images/RoR_sample_webpage.png") %>

You can now use the `cf` <%=vars.dev_console_2%> to review information and administer your app and your <%=vars.product_short%> account. For example, you could edit the `rails_sample_app/manifest.yml` to increase the number of app instances from 1 to 3, and redeploy the app with a new app name and host name.

See the [Manage Your Application with the cf CLI](#cli-manage) section for more information. <%=vars.dev_console_4%>

## <a id='deploy-yourapp'></a>Deploy Your RoR Application ##

This section describes how to deploy your RoR application to <%=vars.product_short%>.

### Prerequisites ###

* [Bundler](http://bundler.io) configured on your workstation
- (**Optional**) [Git](http://git-scm.com) configured on your workstation and a [GitHub](https://github.com) account
* Intermediate to advanced RoR knowledge
- (**Optional**) Robust production web server for Ruby and RoR apps, such as Unicorn
* Your Rails 4.x app that runs locally on your workstation

### Step 1: Add the rails_12factor Gem to Your Gemfile ###

1. Add `gem rails_12factor group: :production` to your Gemfile.

    <p class="note"><strong>Note</strong>:  This gem includes the <code>rails&#095;serve&#095;static&#095;assets</code> gem that enables your Rails app to run in production with the static assets serving functionality. The <code>rails&#095;serve&#095;static&#095;assets</code> gem does this by automatically modifying the <code>config.serve&#095;static&#095;assets</code> configuration parameter from <code>false</code> to <code>true</code>. Do not change the value for this option. For more information, refer to the <a href="https://github.com/heroku/rails_12factor">Rails_12factor GitHub README</a>.</p>

1. Run `bundle install` to update your Gemfile.

### Step 2: (Optional) Configure Automatically Invoked Rake Tasks for Deployed Applications ###

<%=vars.product_short%> does not provide a way to run a Rake task on a deployed application. For <%=vars.product_short%> to automatically invoke a Rake task while an app is deployed, you must include the Rake task in your application and also configure the application start command with the `command` attribute in the application manifest, `manifest.yml`.
The following is an example of how to invoke a Rake database migration task at application startup.

1. Create a file with the Rake task name and the extension `.rake`, and store it in the `lib/tasks` directory of your application.

1. Add the following code to your rake file:

    <pre class="code">
    namespace :cf do
        desc "Only run on the first application instance"
        task :on_first_instance do
            instance_index = JSON.parse(ENV["VCAP_APPLICATION"])["instance_index"] rescue nil
        exit(0) unless instance_index == 0
        end
    end

    </pre>

    This Rake task limits an idempotent command to the first instance of a deployed application.

1. Add the task to the `manifest.yml` file with the `command` attribute, referencing the idempotent command `rake db:migrate` chained with a start command.

    <pre class="code">
applications:
 &#8208; name: my-rails-app
 command: bundle exec rake cf:on_first_instance db:migrate && rails s -p $PORT

    </pre>


For information on how to configure a manifest file, refer to [Step 4: Configure the Manifest File](#yourapp-step4). 

### <a id='yourapp-step3'></a>Step 3: Create and Bind a Service Instance for Your RoR Application ###

This section describes using the CLI to configure a PostgreSQL managed service instance for your app. <%=vars.dev_console_1%>

<%=vars.product_short%> supports two types of service instances:

* Managed services integrate with <%=vars.product_short%> through service brokers that offer services and plans and manage the service calls between <%=vars.product_short%> and a service provider. 
* User-provided service instances enable you to connect your application to pre-provisioned external service instances.

For more information on creating and using service instances, refer to the [Services Overview](./../../devguide/services/index.html) topic.

#### Create a Service Instance ####

Run `cf marketplace` to view managed and user-provided services and plans available to you. 

* Managed services integrate with <%=vars.product_short%> through service brokers that offer services and plans and manage the service calls between <%=vars.product_short%> and a service provider.
* User-provided service instances enable you to bind your application to pre-provisioned external service instances.

For more information on creating and using service instances, refer to the [Services Overview](./../../devguide/services/index.html) topic.

The example shows three of the available managed database-as-a-service providers and their offered plans: `cleardb` MySQL, `elephantsql` PostgreSQL as a Service, and `mongolab` MongoDB-as-a-Service.

<pre class="terminal">

$  cf marketplace
Getting services from marketplace in org Cloud-Apps / space development as <EMAIL>...
OK


service          plans 	description

...
cleardb          spark, boost, amp, shock  	Highly available MySQL for your Apps

...
elephantsql      turtle, panda, hippo, elephant   PostgreSQL as a Service
...
mongolab         sandbox   	Fully-managed MongoDB-as-a-Service
...
</pre>

{'fraction_non_alphanumeric': 0.09309850936289853, 'fraction_numerical': 0.013367991088005942, 'mean_word_length': 4.2512534818941505, 'pattern_counts': {'":': 0, '<': 119, '<?xml version=': 0, '>': 119, 'https://': 8, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7024705', 'n_tokens_mistral': 5644, 'n_tokens_neox': 5180, 'n_words': 2381}