【问题标题】:helper view with haml and simple_form带有 haml 和 simple_form 的辅助视图
【发布时间】:2023-04-11 11:25:02
【问题描述】:

我尝试将 simple_form 帮助器插入到 view 帮助器(对不起双关语)并将其与 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

【问题讨论】:

    标签: haml helpers simple-form


    【解决方案1】:

    只是猜测,但 '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
    

    【讨论】:

      猜你喜欢
      • 2023-03-18
      • 1970-01-01
      • 2011-07-07
      • 2012-06-14
      • 1970-01-01
      • 1970-01-01
      • 2021-11-18
      • 2017-02-07
      • 1970-01-01
      相关资源
      最近更新 更多