tags:

views:

54

answers:

1

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

_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
Pesto
Thanks again Pesto. I think I'm done pestering you for the day :-)
Simon