eclipsis
eclipsis

Reputation: 1550

Use Ruby .erb files in React?

Our site currently uses Ruby and .erb files, and we're about to start a new project using React. Is there a way to use or convert .erb files into a format that works w/ React?

enter image description here

Upvotes: 1

Views: 2151

Answers (1)

Mitch VanDuyn
Mitch VanDuyn

Reputation: 2878

I apologize in advance if I am answering this at too detailed a level. It was hard to tell from your question, how much detailed knowledge you have.

An ERB file has embedded ruby code in tags that look like this:

<% ... some ruby statement %> or <%= ... some ruby expression %>

The ERB processor will replace any of the embedded ruby code with whatever it generates. So for example you might say this:

<ul>
  <% ["animal", "vegetable", "mineral"].each do |n| %>
    <li><%= n %></li>
  <% end %>
</ul>

and the resulting output file would contain

<ul>
  <li>animal</li>
  <li>vegetable</li>
  <li>mineral</li>
</ul>

To answer your question: React is solving a different problem, it is a way of writing any javascript code that runs on the UI in a way that is easy to understand and highly maintainable.

You can use ERB files all you want along with React.

For example you should be able to add React into your system, and then create a couple of interactive UI components with no other change to your system.

All that said, I want to recommend that assuming this is a rails app, and you are moving now to React, you should look into http://reactrb.org which will let you keep coding your react components in Ruby.

There is an excellent tutorial on converting incrementally a working rails app to using react.rb components: https://github.com/loicboutet/reactrb_tutorial

Besides being able to use 1 language (ruby) across your site, react.rb also has big advantages (IMHO) of using your existing rails tool chain, especially WRT to testing.

Upvotes: 5

Related Questions