I'm using Rails 3.2. I have the following code:
# transports_controller.rb
@transports = %w(car bike)
@transports.each do |transport|
instance_variable_set("@#{transport.pluralize}",
transport.classify.constantize.all)
end
# transports/index.html.erb
<% @transports.each do |transport| %>
<h1><%= transport.pluralize.titleize %></h1>
<% @transport.pluralize.each do |transport_item| %>
<%= transport_item.name %><br>
<% end %>
<% end %>
The controller code is correct, but the view code is wrong. @transport.pluralize.each cannot be called literally . Expected result is:
<h1>Cars</h1>
Ferrari<br>
Ford<br>
<h1>Bikes</h1>
Kawasaki<br>
Ducati<br>
How do I do this?
@transport.pluralizeis failing? Need a little more to go on here.@transport.pluralizecannot be called literally. I haven't tested this, but I'm sure that is not the way to write it.instance_variable_set, there is aninstance_variable_get. Is that what you're getting at?