Browse the Ruby on Rails Community.

You are here: Forums Ask a Rails expert String iteration using each_li...

Replytotopic

String iteration using each_line

Posted in Forums : Ask a Rails expert

 
E9bd37cd10777bba199791d18ef414d0

Authority 0
Posting Rating 0
Sign in to rate this post

I have the following code which displays instructions in an ordered list. When it’s rendered in the browser, it outputs all of the instructions at the end in a single line. Am I doing something wrong? See output below for example.

Code:

<% @recipe.instructions.each_line do |instruction| %>
 
  • <= instruction %>

  • < end %>

    Output:

    Instructions:

    1. Roast garlic
    2. test
    3. eat icecream

    4. Roast garlic
      test
      eat icecream

    Rails 3 rc2 ruby 1.9.2dev (2010-07-11 revision 28618)

     
    Profile

    Authority 12
    Posting Rating 0
    Sign in to rate this post

    Hey Man use HTML Tag


      <% @recipe.instructions.each_line do |instruction| %>

      &lt li &gt <%=instruction %> &lt /li &gt

      <% end %>

    Use this Code to Get tour Desired Output

     
    E9bd37cd10777bba199791d18ef414d0

    Authority 0
    Posting Rating 0
    Sign in to rate this post

    Actually it is using html, the code didn’t show up correctly in this post. See line 50 here http://github.com/quattro004/scratches/blob/master/app/views/recipes/show.html.erb

    Any other ideas? Thanks.

     
    Profile

    Authority 12
    Posting Rating 0
    Sign in to rate this post

    I saw your Controller File. In Which method u select the @recipe variable

     
    81539342a46008603039d00c8a7ab03d

    Authority 62
    Posting Rating 99
    Sign in to rate this post

    Reese,

    You have this on line 49: <%= @recipe.instructions.each_line do |instruction| %> but that’s an output ERB tag, it should just be <% @recipe.instructions.each_line do |instruction| %>

    Using the output tag is causing the value of that block to be output, as well as the block itself.

     
    E9bd37cd10777bba199791d18ef414d0

    Authority 0
    Posting Rating 0
    Sign in to rate this post

    Jason,
    Actually I was getting the following deprecation warning on that line (DEPRECATION WARNING: <% > style block helpers are deprecated. Please use <= %>), so I added the = and the warning goes away but the output is still the same. I’m using rails 3 rc2 with ruby 1.9.2dev (2010-07-11 revision 28618).

    I know this has to be something simple but I’m new to ruby and rails :(

     
    Profile

    Authority 37
    Posting Rating 0
    Sign in to rate this post

    If you use the alternate loop style instead, it’ll work:

    for instruction in @recipe.instructions

    In this case, it does the same thing as each_line, and this won’t raise any deprecation errors.

     
    Profile

    Authority 0
    Posting Rating 22
    Sign in to rate this post

    Also to exchange the season when more heat wear, wear less cold, we are not worried about this time in the end is what it is appropriate to wear, what to wear only type it? Do not worry, take a look at the stars beat the street know, just add pieces of uniforms, warm and be stylish.
    http://www.elinkslondon.com/

    Replytotopic

    Other Recent Topics

    Ask a Rails expert : nested application ApplicationController get called intead of children::ApplicationController

    Ask a Rails expert : Best way to structure a database for a large/static dataset

    Ask a Rails expert : Ruby Developer (ROR) - Scottish based (Remote working from within the UK)

    Ask a Rails expert : Above Ground Pool Supplies

    Ask a Rails expert : How to get url params in observer or model in Rails 3.1

    Ask a Rails expert : What can persuade you to hire Junior Ruby devs with significant PHP experience?

    Ask a Rails expert : What industry value does the Ruby or Rails Certification currently have?

    Ask a Rails expert : Louis Vuitton Damier Azur Canvas specially sale ( www.salecheaplouisvuitton.com )

    Ask a Rails expert : ·How to check errors/puts statements from ruby files which are under cronob

    Ask a Rails expert : Louis Vuitton cheap Soft Sided Luggagespecial offer( www.salecheaplouisvuitton.com )

    Formatting Help
    • *bold*       _italics_      
      bq. (quotes)
    • "DSC":http://www.dsc.net
    • * or # (lists)
    or cancel