rubyerbactiveview

Create simple class inside erb template


I'm displaying duplicate blocks of content in an erb (email template) and I thought I would make a simple class that would represent each block.

I tried something like this which works if I manually render the erb, but if I try to send the email I throw.

<%
class EmailBox
  attr_accessor :text, :textLink,
end
x = EmailBox.new
x.textLink = 'https://www.google.com/' 
x.text = 'blah'
@boxes = []
@boxes.push x
%>

<% @boxes.each do |row| %>
         <a style="text-decoration:none;color:#666;" href="<%=row.textLink%>"><%=row.text%></a>
<% end %>

The error I'm getting is:

/Users/x/appname/app/views/clip_mailer/send_clip_with_destination.html.erb:205: class definition in method body
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/actionpack-3.2.13/lib/action_view/template.rb:297:in `module_eval'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/actionpack-3.2.13/lib/action_view/template.rb:297:in `compile'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/actionpack-3.2.13/lib/action_view/template.rb:244:in `block in compile!'
<internal:prelude>:10:in `synchronize'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/actionpack-3.2.13/lib/action_view/template.rb:232:in `compile!'

I'm repeating myself, but this works just fine when I manually render the template by opening it on disk and running ERB.new(file).result(binding)


Solution

  • Somebody answered " If you really want to define a class inside the template you could use a Struct..." and then deleted it. I don't know who but I got an email saying as much. Anyways this lead me down a path of structs, and eventually I found OpenStruct. The conversion is very simple and takes fewer lines:

    <%
    x = OpenStruct.new
    x.textLink = 'https://www.google.com/' 
    x.text = 'blah'
    @boxes = []
    @boxes.push x
    %>
    
    <% @boxes.each do |row| %>
             <a style="text-decoration:none;color:#666;" href="<%=row.textLink%>"><%=row.text%></a>
    <% end %>