Ruby 中的无方法错误

发布于 2024-12-03 06:24:28 字数 721 浏览 3 评论 0原文

我有一些 ruby​​ 代码:

def createCal(cal)
    mod = @on + @off #line creating error.
    @daycount = 0       
    cal
  end

这会生成以下错误: NoMethodError at /calendar undefined method `+' for nil:NilClass file: main.rb location: createCal line: 83

我在 Sinatra 中使用它,所以我可以打印将 @on 和 @off 输出到网页上,我可以确认它们实际上正在加载值。我还在我的 haml 模板中执行了“@ooo = @on + @off”,并生成 7,这是预期的,因为 on 是 4,off 是 3。

有什么想法吗?

更新:

这是我处理@on和@off的方式

post '/calendar' do
  @on = params["on"]
  @off = params["off"]
  @date = params["date"]
  a = Doer.new
  @var = a.makeDate(@date)
  @on = @on.to_i
  @off = @off.to_i
  @ooo = @on + @off
  @cal = a.makeCal(@var)
  haml :feeling
end

I have a bit of ruby code:

def createCal(cal)
    mod = @on + @off #line creating error.
    @daycount = 0       
    cal
  end

This generates the following error: NoMethodError at /calendar undefined method `+' for nil:NilClass file: main.rb location: createCal line: 83

I am using this in Sinatra, and so I can print out @on and @off onto a webpage and I can confirm that they are in fact being loaded with values. I also do a '@ooo = @on + @off' in my haml template and that produces 7, which is to be expected because on is 4 and off 3.

Any ideas?

UPDATE:

Here's how I'm handling @on and @off

post '/calendar' do
  @on = params["on"]
  @off = params["off"]
  @date = params["date"]
  a = Doer.new
  @var = a.makeDate(@date)
  @on = @on.to_i
  @off = @off.to_i
  @ooo = @on + @off
  @cal = a.makeCal(@var)
  haml :feeling
end

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

北城半夏 2024-12-10 06:24:29

您正在访问两个不同的实例变量:

  • post 中的@on 是您的 Sinatra 实例的实例变量。
  • createCal 中的 @on 是来自您的 Doer 实例的实例变量。

要根据需要使用 @on@off,您需要将它们更改为传递给 createCal 方法的参数。像这样的东西:

class Doer
  def createCal(cal, on, off)
    mod = on + off
    # more code...
    cal
  end
end

post '/calendar' do
  a = Doer.new
  date = a.makeDate params['date']
  @cal = a.makeCal date, params['on'], params['off']

  haml :some_template
end

You're accessing two different instance variables:

  • The @on in post is an instance variable for your Sinatra instance.
  • The @on in createCal is an instance variable from your Doer instance.

To use @on and @off like you want, you'll need to change them into arguments passed to the createCal method. Something like this:

class Doer
  def createCal(cal, on, off)
    mod = on + off
    # more code...
    cal
  end
end

post '/calendar' do
  a = Doer.new
  date = a.makeDate params['date']
  @cal = a.makeCal date, params['on'], params['off']

  haml :some_template
end
扛刀软妹 2024-12-10 06:24:29

您的实例变量可能不在该方法的范围内。尝试以下方法来测试这个理论:

def createCal(cal, on, off, daycount)
  mod = on + off #line creating error.
  daycount = 0       
  cal
end

并用以下方式调用它(在您的 /calendar 块中):

createCal(cal, @on, @off, @daycount)

Your instance variables probably aren't in the scope of the method. Try the following to test this theory:

def createCal(cal, on, off, daycount)
  mod = on + off #line creating error.
  daycount = 0       
  cal
end

And call it (in your /calendar block) with:

createCal(cal, @on, @off, @daycount)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文