1
I have the following code:
<table class="table table-bordered table-striped" border="0">
<% @time_line.each do |time_line| %>
<%= render partial: partial_name( get_type(time_line) ), locals: { register: time_line } %>
<% end %>
</table>
@time_line is a variable that concatenates records from multiple models. To complicate matters, for each type of object, I need a different view (it’s a requirement of the project, I can’t change it). So, to make it easier to maintain later, I thought I’d do it this way up. The codes of the methods I used to try to dynamically call the partials:
def partial_name(class_name)
partial_names = {
"Custa" => 'financial',
"Andamento" => 'course',
"Movimentacaoweb" => 'web_movement',
"Publicacao" => 'publication',
"Audiencia" => 'hearing',
"Anotacao" => 'anotation'
}
partial_names[class_name.to_s]
end
def get_type(obj)
obj.class.to_s == 'Mensagem' ? obj.model.to_s : obj.class.to_s
end
For some reason, when I run, it gives the following error:
'nil' is not an Activemodel-compatible Object that Returns a Valid partial path.
Can anyone explain to me what I’m doing wrong?
this message occurs when running <%= render partial:... ? %> def partial_name is inside a helper?
– Gabriel Mazetto
Yes.... inside a helper.
– Pedro Vinícius