# Getting Started with Editmode for Rails

**Install The Gem**

```
gem 'editmode'
```

**Generate your config**

```
rails generate editmode:config {project_id}
```

**Configure caching**

We use the rails cache to store content. This means **if you don't have caching enabled your app will run quite slow**.

```
# Rails 5+
rake dev:cache
```

**Setting up cache expiration webhooks**

When an editor makes changes inside the Content Hub, we need a way to let the Rails app know and expire the cache for that particular content. We use cache expiration webhooks for this. The rails gem comes pre-loaded with endpoints to receive and expire the cache, so all you have to do is configure your project settings to notify the app when content is updated.

**Adding cache expiration endpoints in your project settings**

![](https://editmode.com/docs/images/project-cache-webhook.png)

One caveat is that this method obviously won’t work when working with content locally. To bring your local content in sync with remote changes on the Content Hub, you should enter Editmode and select “Clear Cache”.

**Bringing local content up to date with remote changes.**

![](https://editmode.com/docs/images/editmode-js-clear-cache.png)

{% hint style="info" %}
We are using a method called `delete_matched` to purge your caches when a content gets updated, and this method isn’t supported in `memcached`. We highly recommend using `redis_store` or `file_store`.
{% endhint %}

## Using Editmode in your Rails codebase

* There is full documentation on all of the editmode gem's methods and settings [here](https://docs.editmode.com/editmode-docs/client-libraries/editmode-for-rails).
* Our 2 minute explainer video below should help you understand the

  basics.
* Then, to help you get kicked off, we've also added code snippets

  that you can repurpose for your app.

**Video: Editmode For Rails - Basic Syntax (2 minutes)**

{% embed url="<https://www.youtube.com/watch?v=0MJrdYjMYu4>" %}

## Code Snippets

### Using Editmode for transactional email content in Rails

**Setup Step 1. Create a collection in Editmode with "From", "Subject" and "Body" fields**

![](https://editmode.com/docs/images/email-collection.png)

**Setup Step 2. Add a chunk to the newly created collection.**

![](https://editmode.com/docs/images/email-chunk.png)

#### Create a new mailer using the following code

```ruby
class UserMailer < BaseMailer
  def editmode_email(chunk_identifier,user,variables={})

    # Variables allow us to use things like "Hey {{first_name}}!" 
    # inside the email copy. 

    user_variables = {
      :email      => user.email,
      :first_name => user.first_name,
      :last_name  => user.last_name
    }

    # Also allow other variables to be specified when the method 
    # is triggered
    variables = user_variables.merge(variables)

    to      = user.email
    from    = Editmode.e(chunk_identifier, 'From',    :variables => variables)
    subject = Editmode.e(chunk_identifier, 'Subject', :variables => variables)
    body    = Editmode.e(chunk_identifier, 'Body',    :variables => variables)

    email = mail(
      content_type: 'text/html',
      to:            to,
      from:          from,
      subject:       subject,
      body:          body
    ) 
  end
end
```

#### Congratulations! You can now trigger emails from Editmode with a single line of code!

```
# The first argument here can take either the chunk's identifier or its content key
UserMailer.editmode_email("welcome_email",user).deliver_later
```
