SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Member
    Join Date
    Jun 2014
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Returning multiple lines - Sinatra

    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}\n\n"
    end
    end

    get '/' do
    return_milestones
    end

    end

    Example.run!

  2. #2
    padawan silver trophybronze trophy markbrown4's Avatar
    Join Date
    Jul 2006
    Location
    Victoria, Australia
    Posts
    4,115
    Mentioned
    28 Post(s)
    Tagged
    2 Thread(s)
    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.

    Code ruby:
    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}" %>\n
    <%- end %>
    </pre>
    </body>
    </html>

  3. #3
    padawan silver trophybronze trophy markbrown4's Avatar
    Join Date
    Jul 2006
    Location
    Victoria, Australia
    Posts
    4,115
    Mentioned
    28 Post(s)
    Tagged
    2 Thread(s)
    Oops, the above example will output \n in the page, if removed it will still add each milestone on a new line.

  4. #4
    SitePoint Member
    Join Date
    Jun 2014
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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

  5. #5
    padawan silver trophybronze trophy markbrown4's Avatar
    Join Date
    Jul 2006
    Location
    Victoria, Australia
    Posts
    4,115
    Mentioned
    28 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by bradl3yC View Post
    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.
    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.
    Code ruby:
    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}" %>\n
    <%- 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

  6. #6
    SitePoint Member
    Join Date
    Jun 2014
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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

  7. #7
    SitePoint Zealot
    Join Date
    Feb 2003
    Location
    Akron, OH, USA
    Posts
    106
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    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/show...=1#post5676594) At the end of the file (basics.rb):

    Code Ruby:
    __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}" %>\n
    <%- end %>
    </pre>
    </body>
    </html>

    Then you will still call the view like this:

    Code Ruby:
    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:

    Code Ruby:
    <!doctype html>
    <html>
    <head>
      <meta charset="utf-8">
      <title>Milestones</title>
    </head>
    <body>
    <pre>
    <%- @milestones.each do |milestone| %>
    <%= "#{milestone.title.strip}: #{milestone.number}" %>\n
    <%- end %>
    </pre>
    </body>
    </html>

    In the method you will call:
    Code Ruby:
    get '/' do
      erb :index
    end

    Hopefully this all makes sense!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •