Reputation: 2087
I'm working on an application that creates random sentences. I have it working as a console application, and want to make a Sinatra app which lets me display the sentences on the browser.
I have a variable @grammar that is populated from a form. I want to pass this into a method a few methods which work together to take in a string and generate a random sentence from it using a lot of logic. My rsg.erb file looks like this.
<%= @grammar %>
<%=
rds = read_grammar_defs(@grammar) #get text from file and parse
sds = rds.map { |rd| split_definition rd} #use split definition to make array of strings
tgh = to_grammar_hash(sds) #create hash
rs = expand(tgh) #create sentence
%>
It prints out the @grammar string to the browser just fine, but the output from expand is not being printed to the browser. The expand method is properly generating the sentence as my console shows this:
[2016-11-17 23:48:47] INFO WEBrick 1.3.1
[2016-11-17 23:48:47] INFO ruby 2.3.1 (2016-04-26) [i386-mingw32]
== Sinatra (v1.4.7) has taken the stage on 4567 for development with backup from WEBrick
[2016-11-17 23:48:47] INFO WEBrick::HTTPServer#start: pid=7192 port=4567
::1 - - [17/Nov/2016:23:48:48 -0500] "GET / HTTP/1.1" 200 1485 0.0100
::1 - - [17/Nov/2016:23:48:48 Eastern Standard Time] "GET / HTTP/1.1" 200 1485
- -> /
The waves portend like big yellow flowers tonight.
::1 - - [17/Nov/2016:23:48:51 -0500] "GET /?grammar=Poem HTTP/1.1" 200 8 0.0010
::1 - - [17/Nov/2016:23:48:51 Eastern Standard Time] "GET /?grammar=Poem HTTP/1.1" 200 8
http://localhost:4567/ -> /?grammar=Poem
Where 'The waves portend like big yellow flowers tonight.' is the output of the expand method. I would like to display this on the erb file so it is displayed on the browser.
How can I do that?
Upvotes: 1
Views: 61
Reputation: 4920
Can you try this:
<%= @grammar %>
<%-# Assigning values to the variables in first step %>
<%-
rds = read_grammar_defs(@grammar) #get text from file and parse
sds = rds.map { |rd| split_definition rd} #use split definition to make array of strings
tgh = to_grammar_hash(sds) #create hash
rs = expand(tgh) #create sentence
%>
<%-# Printing it in second step %>
<%= rs %>
Upvotes: 2