Transferring data to other pages in Sinatra

This works fine:

view / index.haml:

%form{:method => 'POST' :action => '/'} %label{:for => 'name'} Name: %input{:type => 'text, :value => @values[:name] || ""} %input{:type => 'submit'} 

app.rb:

 post '/' do @values = params haml :review end 

view / review.rb

 Hello #{params[:name]}! 

However, when I try to send my data in the same view on a different URL, I get an error message or, in other words:

app.rb:

 post '/' do @values = params redirect '/review' end get '/review' do @values = params haml :review end 

Data does not pass, but an error does not occur.

How to send post data through such pages? Ideally, I do not want to create a database.

+4
source share
1 answer

You can save the parameters in the session or explicitly specify the query string. Browser redirection from Sinatra documentation

As stated in the documentation, you can use sessions or convert POST parameters to a query string and use it in the redirect method. A rough example would be:

Say the hash code of the POST parameters inside the '/' block:

 { :name => "Whatever", :address => "Wherever" } 

This hash can be done like this:

 query = params.map{|key, value| "#{key}=#{value}"}.join("&") # The "query" string now is: "name=Whatever&address=Wherever" 

Now use this inside post '/' do

 redirect to("/review?#{query}") 
+6
source

Source: https://habr.com/ru/post/1436278/


All Articles