Thursday, 15 August 2013

ruby - Using variables across 'get' and 'post' routes in Sinatra -


I am using Sinatra and give me an html / one post request for a restaurant order (item, quantity, table_number) In ARB form It's all working brilliantly ....

  get / mainpage 'do @time = time.new.startfime ("% B% e,% Y") erb: Mainpage end post '/ Mainpage' table_number = params [: table_number] .to_i item = param [: items] quantity = param [: quantity] .to_i @order = "# {table_number} ... # {item} ... The problem occurs at the end of # {quantity} "'/" home page " 

The problem occurs when I want to get the request @order variation available in' mainpage ', so Use my html / erb page to:

  & lt; Section class = "table-order" & gt; & Lt;% = @ order% & gt; & Lt; / Section & gt;  

How can I go about doing this?

If your @order is actually only a string and some more complex You do not have an object, you can use the URL parameter to do this. For example:

  get '/ mainpage' do @time = time.New.StraFime ("% B% e,% Y") @order = params [: command] If params [ : Order] erb: mainpage end post '/ mainpage' to table_number = params [: table_number] .to_i item = params [: items] quantity = mercury [[quantity] .to_i command = "# {table_number} ... # Item} ... # {quantity} "(" / mainpage? Order = # {order} ") end  

if your @order something And it's complex object or you need to continue it for more than one redirect, rather than using the session to store it Consider doing it. Something like this:

  get / mainpage 'do @time = time.new.strafttime ("% B% e,% Y") @ order = session [: command] if session [: Order] erb: mainpage end post '/ mainpage' table_number = params [: table_number] .to_i item = params [: items] quantity = param [: quantity] .to_i session [: command] = "# {table_number} # # {Item} ... # {quantity} "('/ mainpage') Redirect to termination  

For more information on generating URLs and using sessions, see.


No comments:

Post a Comment