2012-05-07 33 views
7

Tôi đang làm gì sai với trình trợ giúp này cho mẫu HAML của tôi?kết quả đầu ra haml_tag trực tiếp vào mẫu 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 

Đây là lỗi:

display_event
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. 

Các mẫu được gọi như thế này:

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

Nếu tôi đã sử dụng đánh dấu thường xuyên nó sẽ mở rộng như sau

%li.fooclass 
    %b Foo 
    %i Bar 

Trả lời

10

Các đầu mối trong erro nhắn r:

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

Từ các tài liệu cho 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 .

Để khắc phục nó, hoặc là thay đổi Haml của bạn để:

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

(ví dụ: sử dụng - điều hành thay vì =), hoặc thay đổi phương thức sử dụng 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 

Điều này sẽ làm phương thức trả về một chuỗi, sau đó bạn có thể hiển thị với = trong Haml của bạn.

Lưu ý rằng bạn cần thực hiện chỉ một những thay đổi này, nếu bạn khiến cả hai sẽ hủy nhau và bạn sẽ không thấy gì hiển thị.

Các vấn đề liên quan