I’m having a hard time rendering a view in Haml,using MongoMapper for a small Sinatra app I’m working on.
I have this here which I know the query is not bad:
get '/admin' do
protected!
@title = "admin"
@total_items = Item.all(:order=>:itemtype.desc)
haml :admin, :locals => @total_items
end
Now, the problem is , I’m trying to use the @total_items instance variable in my Haml view to be able to output the keys in an ordered list after retrieving each item with the loop:
%div
%img
- @total_items.each do |item|
%h3= item
and I’m stuck.How do I access the keys? Thanks!
There are some issues with your code at hand:
If you use instance variables you don’t need to use
:locals. They are accessible in your views anyway. So either usetotal_itemsand make use of:localsor use@total_itemsand access them as that in your views. BTW,:localsare used like that:then you can access in admin.haml your
total_itemsasitemsBack to your question: In your admin.haml file there is a small error that is easy to fix: Make sure you get the indentation right. After all this is what haml is about:
Edit:
You could even keep it more simple by querying the items from your view:
PS: What is the
%imgtag good for? It’s not used in your example.