Reputation: 7633
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)
Upvotes: 2
Views: 834
Reputation: 7633
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 %>
Upvotes: 0
Reputation: 458
You can't, as far as I'm aware, define classes within erb. Even if you could, I'd question the design logic behind such an approach - in general you want to keep a wall of separation between your data and templates.
All of that said, you can accomplish something similar with a method which returns a list or hash, etc:
<% def get_data; return {:text => 'blah', :textLink => 'http://www.google.com'}; end %>
<%= get_data[:textLink] %>
Upvotes: 1