views:

32

answers:

2

I have a block of this code in my helper:

if !shop.directions.blank?
     "<ul>".html_safe +
        shop.directions.each do |direction|
          "<li>#{direction.direction}</li>".html_safe
        end +
     "</ul>".html_safe
else
    "No directions available.".html_safe
end +

It's showing memory location of direction.direction like

#<Direction:0xab3c6d0>#<Direction:0xa32c6d0>

instead of the value of it like

1. Take bus no. 3
2. Take train towards Lydia Ave.

Thanks.

UPDATE 1

Now I changed it to this:

spot.directions.flatten.map do |direction|
          "<li>".html_safe + direction.direction + "</li>".html_safe
        end

using the flatten.map. But then, it shows the entire code on the browser:

 <li>Take bus no. 3</li><li>Take train towards Lydia Ave.</li>

Yes, including the <li><li>

A: 

This looks like you're storing a Direction object (that doesn't have a custom to_s method) in direction.direction, instead of a string. Try outputting direction.direction.inspect to get a better idea of what's going on. Also keep in mind that ruby-debug can be a great tool over debugging by printing out values.

My guess, without looking at the code, would be that you might have an array of arrays of direction objects, like [[direction1],[direction2]] instead of the flattened array that you're expecting [direction1, direction2].

Update: Array#map returns another array -- what you want to do is build up a string, not return an array, so consider either explicitly building up the string, or calling join on the resultant array.

Ben Taitelbaum
Please see my update.
Victor
Thanks guys, I think it's better for me to just forget about putting it in helper, but in the view instead. Save more troubles.
Victor
A: 

Your block seems to be fine, did you check what kind of object Direction#direction returns? If it returns a Direction object, then that's exactly what you would get.

Answer for your update: Any combined/reconstructed string loses its "html safe" status, and it gets escaped on the output. You should call html_safe on the final result, and inside the block, escape risky strings manually with h(risky_part)

edgerunner
Please see my update.
Victor
Please see 'my' update.
edgerunner
I'm sorry, removing .html_safe or adding it doesn't solve the problem. <li></li> is still shown in the output. It's like a "pre" text now.
Victor
I meant that your helper function should return like `return result.html_safe`
edgerunner