2012-05-07 6 views
7

Cosa sto facendo di sbagliato con questo helper per il mio modello HAML?haml_tag restituisce direttamente il modello Haml

def display_event(event) 
    event = MultiJson.decode(event) 
    markup_class = get_markup_class(event) 
    haml_tag :li, :class => markup_class do 
     haml_tag :b, "Foo" 
     haml_tag :i, "Bar" 
    end 
    end 

Questo è l'errore:

haml_tag outputs directly to the Haml template. 
Disregard its return value and use the - operator, 
or use capture_haml to get the value as a String. 

Il modello sta chiamando display_event in questo modo:

- @events.each do |event| 
    = display_event(event) 

Se stavo usando markup regolare sarebbe ampliare al seguente

%li.fooclass 
    %b Foo 
    %i Bar 

risposta

10

L'indizio è nell'erro messaggio R:

Disregard its return value and use the - operator, 
or use capture_haml to get the value as a String. 

Dalla documentazione per haml_tag:

haml_tag outputs directly to the buffer; its return value should not be used. If you need to get the results as a string, use #capture_haml .

per risolvere il problema, modificare il vostro Haml a:

- @events.each do |event| 
    - display_event(event) 

(vale a dire utilizzare l'operatore di - invece di =), o modificare il metodo da utilizzare capture_haml:

def display_event() 
    event = MultiJson.decode(event) 
    markup_class = get_markup_class(event) 
    capture_haml do 
    haml_tag :li, :class => markup_class do 
     haml_tag :b, "Foo" 
     haml_tag :i, "Bar" 
    end 
    end 
end 

Questo renderà il metodo di restituire una stringa, che è possibile visualizzare con = nel vostro Haml.

Nota è necessario effettuare solo uno di di queste modifiche, se si effettuano entrambi si annulleranno a vicenda e non verrà visualizzato nulla.