为什么当我调用 Shoes#'visit' 时会丢失实例变量?

发布于 2024-08-01 18:06:59 字数 388 浏览 7 评论 0原文

我确信这与鞋>中提到的复杂性有关。 手册> 规则,但我就是不明白。 如果有人愿意解释为什么在下面的代码中 @var == nil ... 我认为我可以使用访问在应用程序中的不同视图之间切换,但如果我丢失所有状态,这将不起作用。

class MyShoe < Shoes
  url '/', :index
  url '/somewhere', :somewhere

  def index
    @var = para link( "go somewhere", :click => "/somewhere" )
  end

  def somewhere
    para "var = #{@var.inspect}"  
  end
end

Shoes.app

I'm sure this has to do with the intricacies mentionned in Shoes > Manual > Rules but I just don't get it. If someone would care to explain why @var == nil in the following code ...
I thought I could use visit to switch between different views in my application but that won't work if I lose all state.

class MyShoe < Shoes
  url '/', :index
  url '/somewhere', :somewhere

  def index
    @var = para link( "go somewhere", :click => "/somewhere" )
  end

  def somewhere
    para "var = #{@var.inspect}"  
  end
end

Shoes.app

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

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

发布评论

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

评论(1

阳光下慵懒的猫 2024-08-08 18:06:59

_为什么他自己回答了这个问题,我稍后会讨论这个问题。 首先,在不同 url 之间传递数据(特别是字符串)的最简单方法是这样的:

class MyShoe < Shoes
  url '/', :index
  url '/somewhere/(\d+)', :somewhere

  def index
    @var = para link( "What is 2 + 2?", :click => "/somewhere/4" )
  end

  def somewhere(value)
    para "2 + 2 = #{value}"  
  end
end

Shoes.app

它将匹配正则表达式的子组,并将匹配的字符串作为参数传递给方法。 偶尔有用,但很快就会变得笨拙。 另一种解决方案是使用常量或类变量,如 _为什么在这里解释

<块引用>

好的,所以进一步闲逛,看起来所有实例变量都得到了

在 Shoes 子类中每个方法的开头处进行擦除。
我想没关系。 那么获取数据的首选方式是什么
是从一个鞋 URL 共享到另一个鞋 URL 的吗? 从一页传递它
URL 中的下一个本身就可以工作——如果它是一个字符串。 如果不是
一个字符串,你应该使用什么——@@class_variables?

当然你可以使用类变量。 那些
保证始终坚持
应用程序的生命周期。 或者一个常数。

此外,Shoes 还附带 SQLite3、数据
可以通过那里。

在您的示例中,它看起来像这样:

class MyShoe < Shoes
  url '/', :index
  url '/somewhere', :somewhere

  def index
    @@var = para link( "go somewhere", :click => "/somewhere" )
  end

  def somewhere
    para "var = #{@@var.inspect}"  
  end
end

Shoes.app

_why himself has answered this issue, and I'll get to that in a minute. First, the simplest way to pass data (specifically, strings) between different urls is like this:

class MyShoe < Shoes
  url '/', :index
  url '/somewhere/(\d+)', :somewhere

  def index
    @var = para link( "What is 2 + 2?", :click => "/somewhere/4" )
  end

  def somewhere(value)
    para "2 + 2 = #{value}"  
  end
end

Shoes.app

It will match the subgroups of the regex and pass the matching strings as parameters to the method. Occasionally useful, but it gets unwieldy in a hurry. The other solution is to use constants or class variables, as _why explains here:

OK, so fooling around further it looks like all instance variables get

wiped at the beginning of every method within a Shoes subclass.
That's OK I guess. So what's the preferred way to have some data
that's shared from one Shoes URL to another? Passing it from one page
to the next in the URL itself works -- if it's a string. If it's not
a string, what should you use -- @@class_variables?

Sure you could use a class var. Those
are guaranteed to persist throughout
the life of the app. Or a constant.

Also, Shoes comes with SQLite3, data
can be passed there.

In your example, it would look like this:

class MyShoe < Shoes
  url '/', :index
  url '/somewhere', :somewhere

  def index
    @@var = para link( "go somewhere", :click => "/somewhere" )
  end

  def somewhere
    para "var = #{@@var.inspect}"  
  end
end

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