ruby on rails - How to iterate instance variables within instance variables in view? -
i'm using rails 3.2. have following code:
# transports_controller.rb @transports = %w(car bike) @transports.each |transport| instance_variable_set("@#{transport.pluralize}", transport.classify.constantize.all) end # transports/index.html.erb <% @transports.each |transport| %> <h1><%= transport.pluralize.titleize %></h1> <% @transport.pluralize.each |transport_item| %> <%= transport_item.name %><br> <% end %> <% end %>
the controller code correct, view code wrong. @transport.pluralize.each
cannot called literally . expected result is:
<h1>cars</h1> ferrari<br> ford<br> <h1>bikes</h1> kawasaki<br> ducati<br>
how do this?
just use instance_variable_set
, there instance_variable_get
available.
Comments
Post a Comment