如何在Sinatra中使用MongoMapper访问Mongo数据库键以在Haml视图中呈现值?

时间:2021-03-28 10:59:50

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:

我很难在Haml中渲染视图,使用MongoMapper来处理我正在研究的小型Sinatra应用程序。我在这里有这个,我知道查询不错:

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:

现在,问题是,我正在尝试在Haml视图中使用@total_items实例变量,以便在使用循环检索每个项目后输出有序列表中的键:

%div 
  %img     
  - @total_items.each do |item|
  %h3= item

and I'm stuck.How do I access the keys? Thanks!

我被卡住了。如何访问密钥?谢谢!

2 个解决方案

#1


2  

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 use total_items and make use of :locals or use @total_items and access them as that in your views. BTW, :locals are used like that:

如果使用实例变量,则不需要使用:locals。无论如何,它们都可以在您的视图中访问因此,要么使用total_items并使用:locals,要么使用@total_items并在视图中访问它们。顺便说一句,:本地人就是这样使用的:

haml  :admin, :locals => {:items => total_items}

then you can access in admin.haml your total_items as items

然后你可以在admin.haml中访问你的total_items作为项目

Back 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:

回到你的问题:在你的admin.haml文件中有一个很容易修复的小错误:确保缩进正确。毕竟这是haml的意思:

%div 
  %img     
  -items.each do |item| # I use the above mentioned :locals with items instead of total_items
    %h3= item

Edit: You could even keep it more simple by querying the items from your view:

编辑:您甚至可以通过查看视图中的项目来使其更简单:

%div 
  %img     
  -Item.all(:order=>:itemtype.desc).each do |item|
    %h3= item

PS: What is the %img tag good for? It's not used in your example.

PS:%img标签的优点是什么?它没有在你的例子中使用。

#2


1  

So, what I'm doing is this and it works perfectly:

所以,我正在做的是这个并且它完美地工作:

%div      
-Item.all.each do |item|
%ul
  %li= item[:itemtype]
  %li= item[:desc]
  %li= item[:size]
  %li= item[:price]
  %li= item[:image]

#1


2  

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 use total_items and make use of :locals or use @total_items and access them as that in your views. BTW, :locals are used like that:

如果使用实例变量,则不需要使用:locals。无论如何,它们都可以在您的视图中访问因此,要么使用total_items并使用:locals,要么使用@total_items并在视图中访问它们。顺便说一句,:本地人就是这样使用的:

haml  :admin, :locals => {:items => total_items}

then you can access in admin.haml your total_items as items

然后你可以在admin.haml中访问你的total_items作为项目

Back 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:

回到你的问题:在你的admin.haml文件中有一个很容易修复的小错误:确保缩进正确。毕竟这是haml的意思:

%div 
  %img     
  -items.each do |item| # I use the above mentioned :locals with items instead of total_items
    %h3= item

Edit: You could even keep it more simple by querying the items from your view:

编辑:您甚至可以通过查看视图中的项目来使其更简单:

%div 
  %img     
  -Item.all(:order=>:itemtype.desc).each do |item|
    %h3= item

PS: What is the %img tag good for? It's not used in your example.

PS:%img标签的优点是什么?它没有在你的例子中使用。

#2


1  

So, what I'm doing is this and it works perfectly:

所以,我正在做的是这个并且它完美地工作:

%div      
-Item.all.each do |item|
%ul
  %li= item[:itemtype]
  %li= item[:desc]
  %li= item[:size]
  %li= item[:price]
  %li= item[:image]