def partial
edible_type.class.name.underscore ##edible type can be apple or orange
end
<% @edibles.each do |edible| %>
<%= render( :partial => "edibles/#{edible.partial}", :locals => {:edible => edible}) %>
<% end %>
nicer paths to partial views of subclasses when rendering a collection
-
26-06-2022 - |
Domanda
Suppose I have:
class Edible
end
class Apple < Edible
end
class Orange < Edible
end
I am rendering a collection of these edibles, regardless of their actual type:
<%= render @edibles %>
This works, but Rails wants me to place these views in apples/apple
and oranges/orange
.
Is there a way to tell Rails to look for views in the same folder, i.e. edibles/apple
and edibles/orange
?
Soluzione
Altri suggerimenti
You can render collection without using each:
<%= render partial: "edibles/edible", collection: @edibles, as: :edible %>
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow