AMB
AMB

Reputation: 335

How do I pass an existing Variable that changes at refresh to a param in a post method using sinatra

I'm using sinatra as my web framework and right now I have

<p><%= @sentence %></p>

<form action='/' method='POST'>

  <button type='submit'>Save Story</button>
</form>

in my erb file. The variable at @sentence changes at refresh. When I hit the save Story button I want it to create a param in the post method that is equal to @sentence so that I can save @sentence to the main page. Is there anyway to do this without javascript?

ANSWERED

I used

`<div class="row">
  <form action='/' method='POST'>
    <input id="sentence" type="hidden" name="sentence" value= "<%= @sentence %>" >
    <button type='submit'>Save Story</button>
  </form>
</div>`

its still only taking the first word on one of the 4 pages but there must be something else going on there.

Upvotes: 0

Views: 1352

Answers (2)

dav3ydark007
dav3ydark007

Reputation: 54

You need to create a hidden input field with the value set to w.e @sentence is

<p><%= @sentence %></p>

<form action='/' method='POST'>
 <input type="hidden" name="sentence" value="<%= @sentence %>" />
 <button type='submit'>Save Story</button>
</form>

This will give the form something to pass that you can grab with post elsewhere, hope that helps, just put your variable where the ... is and be sure to tell it the language, here is a php example on how to add a varaible to a value.

 value="<?php echo $state; ?>"

Here I'm basically telling the browser to echo(print) the state variable between the " and " using php start and end to initiate the language and end it, The hidden type input field is invisible to users and they cannot edit it, its a background trick you can use to pass information, it acts as a text field.

Information on hidden fields: http://www.blooberry.com/indexdot/html/tagpages/i/inputhidden.htm

When you select an answer, please edit your main post to display ANSWERED and the updated code so users can see what you decide to use.

Upvotes: 1

Uri Agassi
Uri Agassi

Reputation: 37409

In sinatra you can do this:

<p><%= @sentence %></p>

<form action='/' method='POST'>
 <input type="hidden" name="sentence" value="<%= @sentence %>" />
 <button type='submit'>Save Story</button>
</form>

Upvotes: 0

Related Questions