I am brand new to programming/ruby. I am trying to return milestone information from github using sinatra. What I have found is that I am only getting it to return one line. When I do this in a command line without sinatra, it works just fine. This is my first attempt at writing anything outside of a tutorial, so go easy on me Any help is appreciated.
require ‘sinatra/base’
require ‘octokit’
class Example < Sinatra::Base
def initialize
super
client = Octokit::Client.new(login: ENV[‘GITHUB_LOGIN’], password: ENV[‘GITHUB_PW’])
@milestones = client.list_milestones(‘login/repo’, state: ‘Open’)
end
def return_milestones
arr ||=
@milestones.each do |milestone|
arr << “#{milestone.title.strip}: #{milestone.number.to_s}”
end
arr.each do |x,y|
return "#{x}: #{y}
"
end
end
get ‘/’ do
return_milestones
end
end
Example.run!
Hi there,
If you’re hitting this route from a browser the reason multilines aren’t showing is that in HTML it’s expecting <br> for newlines not new lines in the source. Alternatively you could wrap the content in a <pre> element to preserve formatting.
It’s up to you where you want to do the formatting but it might just be simpler to move this to a template.
get '/' do
erb :index
end
#index.erb
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Milestones</title>
</head>
<body>
<pre>
<%- @milestones.each do |milestone| %>
<%= "#{milestone.title.strip}: #{milestone.number}" %>\
<%- end %>
</pre>
</body>
</html>
Oops, the above example will output
in the page, if removed it will still add each milestone on a new line.
Hey Mark,
Thanks for the reply!
When using erb, do you need to include that as a separate file in a views folder? I haven’t spent much time breaking up my code in a nice neat form, just trying to understand basics currently.
Thanks,
Brad C
Hey Brad,
If you call erb :index inside sinatra it will render views/index.erb
You can also pass a string to the erb method though if you want it inline.
get '/' do
erb '
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Milestones</title>
</head>
<body>
<pre>
<%- @milestones.each do |milestone| %>
<%= "#{milestone.title.strip}: #{milestone.number}" %>\
<%- end %>
</pre>
</body>
</html>'
end
But then you need to worry about escaping and don’t get as nice syntax highlighting in your text editor.
http://www.sinatrarb.com/intro.html
Seems like Sinatra isn’t liking the word end.
SyntaxError at /
basics.rb:34: syntax error, unexpected keyword_end ; - end ^ basics.rb:39: syntax error, unexpected keyword_ensure, expecting keyword_end basics.rb:43: syntax error, unexpected end-of-input, expecting keyword_end end;end;end;end ^
file: template.rb location: class_eval line: 264
Are you trying to place the view inline or are you calling in a separate file?
If you are trying to place the views inline, you need to do something like this: http://www.sinatrarb.com/intro.html#Inline%20Templates
Using what markbrown4 gave you earlier (http://www.sitepoint.com/forums/showthread.php?1214046-Returning-multiple-lines-Sinatra&p=5676594&viewfull=1#post5676594) At the end of the file (basics.rb):
__END__
@@index
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Milestones</title>
</head>
<body>
<pre>
<%- @milestones.each do |milestone| %>
<%= "#{milestone.title.strip}: #{milestone.number}" %>\
<%- end %>
</pre>
</body>
</html>
Then you will still call the view like this:
get '/' do
erb :index
end
If you are separating the views you need to create a views folder. Inside the folder create an index.erb file:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Milestones</title>
</head>
<body>
<pre>
<%- @milestones.each do |milestone| %>
<%= "#{milestone.title.strip}: #{milestone.number}" %>\
<%- end %>
</pre>
</body>
</html>
In the method you will call:
get '/' do
erb :index
end
Hopefully this all makes sense!