haml和simple_form的辅助视图

时间:2011-12-08 23:48:20

标签: haml helpers simple-form

我尝试将 simple_form 帮助器插入视图助手(对双关语抱歉)并将其与 Haml :: Helpers 一起使用,例如:

def front_box_search_html(form_object)
    my_custom_wrapper() do
        non_haml do
            simple_form_for( form_object ||= Form::Search.new, :url => front_search_path ) do |f|
                haml_concat f.input :phrase
                haml_concat f.button :submit
            end
        end
    end
end

但是这段代码没有正确呈现:显示所有html代码(各种html标签包装,输入和标签),但没有标签,正如您所看到的那样是结果(测试使用RSpec,但在浏览器中是相同的事情):

Failure/Error: helper.capture_haml{ helper.front_box_search_html }.should have_xpath("/html/body/div[@*]/form")
expected following text to match xpath /html/body/div[@*]/form:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html>
 <body>
  <div class= "boxSearch box">
   <p class = "sum">search title</p>
   <div class = "row string optional">
    <label class = "string optional" for="form_search_phrase"> word</label>
    <input class="string optional" id="form_search_phrase" name="form_search[phrase]" type="text">
   </div>
   <div class = "row buttons last">
    <input class = "button" id="form_search_submit" name="commit" type="submit" value="search">
   </div>
  </div>
 </body>
</html>

老实说我还没有完全理解如何使用Haml :: Helpers的各种方法,关于文档很差。


我的环境:rails 3.0.7,haml 3.1.4,simple_form 1.5.2

1 个答案:

答案 0 :(得分:0)

只是一个猜测,但'simple_form_for'助手也输出html所以我认为它应该是:

haml_concat simple_form_for( form_object ||= Form::Search.new, :url => front_search_path ) do |f|
  haml_concat f.input :phrase
  haml_concat f.button :submit
end