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?

È stato utile?

Soluzione

  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 %>

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
scroll top