我试图在我的Ruby应用程序中使用Haml文件,但是我得到了这个错误:
NoMethodError at /issues
undefined method `map' for "localhost:27017":String
index.haml in block in singleton class
9. -@issues.each do |issue|
app.rb in block in <class:App>
14. haml :"issues/index"这是我的index.haml文件:
%h1 All Issues
%table.table.table-hover
%thead
%tr
%th Title
%th Description
%th Created at
-@issues.each do |issue|
%tr
%td
%a(href="/issues/#{issue.id}/edit")=issue.name
%td=issue.description
%td=issue.id.generation_time.ago_in_words
-unless @issues.any?
%tfoot
%tr
%th
There are no issues.
%a.btn.btn-primary(href="/issues/new") Create one 下面是app.rb文件:
require_relative "models/issue"
class App < Sinatra::Base
enable :sessions
register Sinatra::Flash
get "/" do
"redirect/issues"
end
get "/issues" do
@issues = Issue.all
haml :"issues/index"
end
end问题似乎出在我的-@issues.each do |issue|循环上,但我确信问题出在Ruby Syntax或Haml,还是其他什么地方。我还在我的/models目录中创建了一个issue.rb文件,如下所示:
class Issue
include Mongoid::Document
include Mongoid::Timestamps::Updated
field :name, type: String
field :description, type: String
end编辑:我想我的问题是问题没有初始化。我收到一个错误,上面写着:
uninitialized constant Issue::Mongoid (NameError)发布于 2015-07-25 10:39:46
我注意到的第一件事是,在这个假设和它后面的Ruby代码之间没有留下空格。我不确定这是否真的会对Haml的解析方式产生影响,但all the examples in the Haml reference leave a space,例如。
-@issues.each do |issue|应该是
- @issues.each do |issue|接下来要检查Issues.all的值,可以使用pry之类的工具进行调试,也可以使用warn将其输出到控制台。
@issues = Issue.all
warn @issues或
binding.pry
@issues = Issue.allindex.haml中的-unless @issues.any?行也可以从更改中受益。any?通常是使用块调用的,- if @issues.empty? (IMO)比使用较少使用的方法版本的unless更容易理解。
https://stackoverflow.com/questions/31620592
复制相似问题