I have a string with column names and I want to print out the keys and value开发者_Python百科s in this order.
# controller
fields = "name, year, title"
@blas = Bla.find(23, :select=>fields)
# view
<% @blas.attributes.each do |k,v| %>
<%=k %>:<%=v %><br>
<% end %>
The result I get is in this order "name, title, year" but I want "name, year, title", like I defined it in fields. How can I do that?
You could cycle through them like this:
# controller @fields = "name, year, title" @blas = Bla.find(23, :select=>@fields) # view <% @fields.split(',').map(&:strip).each do |key| %> <%= k %>:<%= @blas[k] %><br /> <% end %>
精彩评论