0

After clicking a link, I want to redirect the user to another HAML view and send some local data along with it. How do I do this?

adijo
  • 1,450
  • 1
  • 14
  • 20

1 Answers1

2

If I get your question right, you want to display a page with a link. If a user clicks the link he gets forwarded to the link target and you want to fetch information from the client when the request hits in.

If so, it depends what kind of information you want to send. If you have the information on server side when rendering the page, you would put them straight as query string parameters to the target url of the link, with HAML like this:

%a{ :href => "/target?var1=#{@var_x}&var2=#{@var_y}", :title => "your link" }

would become in HTML:

<a href="/target?var1=value_x&var2=value_y">your link</a>

If the user follows the link, you would fetch the information in your Sinatra route like this:

get "/target" do
  puts params[:var1] # => value_x
  puts params[:var2] # => value_y
end
maddin2code
  • 1,334
  • 1
  • 14
  • 16
  • When a user clicks a link, I want to pass a ruby object to another HAML page (such that I can then access the object there via ```locals```) and display it. Is this possible? – adijo Sep 26 '14 at 12:14
  • I understand now, but still, the Sinatra route (which is in charge for the page behind the link the user clicked) has to pass the object to Haml via local. See this question: http://stackoverflow.com/questions/9504094/how-to-pass-an-argument-when-calling-a-view-file This means your Sinatra route needs the object you want to pass beforehand. How you do this depends on the object / information you want to pass. – maddin2code Sep 26 '14 at 14:34
  • I have one local route running on my PC. Can I pass the parameters to another `.rb` sinatra file that will handle this? I tried passing it to `/different_route` for example, but it gave me an error. – adijo Oct 04 '14 at 08:25