Why am I losing instance variables when I call Visit #?

I'm sure it has to do with the niceties mentioned in Shoes> Manual> Rules, but I just don't get it. If anyone wants 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 doesn'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

      

+2


source to share


1 answer


_

why answered this problem myself and I'll get to that in a minute. First, the simplest way to pass data (in particular strings) between different URLs is as follows:

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 subgroups of the regex and pass the appropriate strings as parameters to the method. Sometimes useful, but it gets unwieldy in a hurry. Another solution is to use constants or class variables as _

why is explained here
:



OK, so cheat further, it looks like all instance variables get

     

wipe at the beginning of each method in the Shoes subclass.
  It's ok I think. So what's the preferred way to get some data
  that is 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 is not a string, what should you use - @@ class_variables?

You can of course use the var class. Those are guaranteed the life of the application. Or a constant.

In addition, shoes comes with SQLite3, data can be transferred 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

      

+2


source







All Articles