nilatti
nilatti

Reputation: 578

How to interpolate ruby inside of an interpolated bit of ruby in ERB

I'm trying to create a situation where one user makes message templates and another one can plug in values. I'm using the best_in_place gem, which will allow a user to edit the message on the show page.

The problem is this. When I call the message, with the required erb to make the gem work, it treats all of this as a regular string, not as ruby.

This is unclear, I'm sorry.

Here's the code.

@announcement.content = "The <%= best_in_place @announcement, :train %> is arriving in five minutes."

/show.html.erb

<%= @announcement.content %>

I want it to put "The click to set train is arriving in five minutes." and if the user clicks where it says "click to set train," a text field will open for them to edit (this is something the best-in-place gem does).

Instead, it puts "The <%= best_in_place @announcement, :train %> is arriving in five minutes."

I understand why it is doing this, but I don't know how to make it instead interpret the ruby I'm trying to pass in.

Ideas?

Upvotes: 1

Views: 1948

Answers (5)

nilatti
nilatti

Reputation: 578

I think the fact that I wasn't clear made it hard to answer this question. What I'm doing is transforming user-inputted text (using a method in the model, called by the controller) to replace certain keywords with erb tags that call the best_in_place plugin. In my view, when presenting this content to another user, I wanted to call this content, which is saved as an attribute in the database, in such a way that it would render correctly for the other user to have the best_in_place functionality active.

Here's what I ended up doing. It is working, but if you have better ideas, please let me know.

In the announcements#create view, the user creates an announcement with certain pre-defined blocks of bracketed text as well as free-input text. For example, they might write "[train] is leaving from [platform] in [time] minutes."

When they hit save, the controller's create action calls the construct_message method from the model. It looks like this:

def construct_message(msg)
    msg.gsub! '[train]', '<%= best_in_place @announcement, :train_id, :as => :select, collection: Train::list_trains, place_holder: "Click here to set train." %>' #note: list_trains and list_platforms are methods on the model, not really important...
    msg.gsub! '[platform]', '<%= best_in_place @announcement, :platform_id, :as => select, collection: Platform::list_platforms, placeholder: "Click here to set platform." %>'
    msg.gsub! '[time]', '<%= best_in_place @announcement, :number_of_minutes, placeholder: "Click here to set." %>'
end

Then, when I want to show that attribute in my view, I'm using render :inline, like this.

on announcements/:id

<p id="notice"><%= notice %></p>

<p>
  <strong>Content:</strong>
  <% announcement = @announcement %>
    <%= render :inline => announcement.content, locals: { :announcement => announcement } %>
</p>

This allows the erb call that I wrote into the attribute to be functional. Also note that I'm choosing to use a local rather than instance variable here; this is because in announcements#index, I also render this text and the table there uses local variables.

Upvotes: 0

Giuse
Giuse

Reputation: 126

TL;DR: ERB is not Ruby, and Rails uses both at different times.

You want simple Ruby string interpolation here:

@announcement.content = "The #{best_in_place @announcement, :train} is arriving in five minutes."

This is unclear, I'm sorry.

Not to worry, the Rails framework throws so many different new concepts at you it can be frustrating for newcomers.

Start from this: the Ruby framework builds the answer to the user's browser from a collection of resources Each file is evaluated by an interpreter for its own language. The trick is: look at the extension.

Files ending in .coffee will be compiled into javascript, files ending in .scss will become CSS, and in the same way files ending in .erb will yield HTML.
ERB is a language composed of mostly HTML already, plus a tag that allows you to interpolate Ruby. ERB stands for Embedded Ruby.

What about files ending in .rb, like the file in which you (surely) are evaluating @announcement.content = "The <%= best_in_place[...]" (a controller, I guess)? Well, that's just pure Ruby :) that's why the ERB interpolation syntax <%= ... > is not recognized.

What you want to do in the controller, is (as you're trying to do) preparing the data for the view. The ruby in the <%= ... > tag in ERB will have access to the controller's instance variables, i.e. the variables with an @ in front defined in the controller. But to define those, inside the controller, you should rely on Ruby alone.

Take-home message:

Be aware of which language you are writing in at each moment. For example:

# show.html.erb
<p>Here is ERB, which will be interpreted straight into HTML</p>
<% "Inside the '<% ...' tag is Ruby, but results won't show up in the HTML because there's no '<%='."%>
<% which_language = "Ruby" # Even variable assignments, and comments, do work %> 
<%= "Inside the '<%=' tag, you're writing and interpolating #{which_language} :)" %>

Upvotes: 0

tadman
tadman

Reputation: 211540

The Rails way to do this:

@announcement.content_type = :arriving

Later:

<%= render(partial: @announcement.content_type)

In _arriving.erb:

The <%= best_in_place @announcement, :train %> is arriving in five minutes.

Upvotes: 0

Jordan Running
Jordan Running

Reputation: 106017

Use regular old string interpolation:

@announcement.content = "The #{best_in_place @announcement, :train} is arriving in five minutes."

Upvotes: 2

Stephen Crosby
Stephen Crosby

Reputation: 1261

You can use ERB to render any ERB template string. In this case something like:

<%= ERB.new(@announcement.content).result %>

Although you likely won't have access to all your Rails helpers, etc.

Upvotes: 1

Related Questions