1

我正在为处理调查数据的 Radiant CMS 做一些扩展。我正在尝试使用 rails 在预定义的半径标签内提供的 form_for、fields_for 和各种帮助器。这些标签将在 Radiant 页面上生成调查。

这是我与 Radiant 集成的想法:

<r:survey id="200">
  <r:survey:form>                 #<-- call to form_for
    <r:survey:questions:each>     # <-- calls fields_for
      <r:question>                # <-- renders question
      <r:answer_field>            #<-- renders input via text_field, text_area, etc
    </r:survey:questions:each>
  </r:survey:form>
</r:survey>

所以当我调用 <r:survey:form> 时,它会生成 <form> 标签。我可以通过手动制作 html 来做到这一点,但我想使用 form_for 助手等。

有什么办法可以实现以下目标:

# ------ <r:survey:form> -----------------------------
  tag 'survey:form' do |tag|
    # call form_for which would render form header and open <form> tag
    tag.expand
    # form_for would end here, closes </form>
  end

# ------ <r:survey:questions>----------------------------
  tag 'survey:questions' do |tag|
    tag.expand
  end

# ------ <r:survey:questions:each>------------------------
  tag 'survey:questions:each' do |tag|
    result = []
    survey = tag.locals.survey
    # should call fields_for here
    survey.survey_questions.sort_by{|q| q.order}.each do |question|
      tag.locals.question = question
      result << tag.expand
    end
    # end of fields_for
    result
  end 

我希望这能解释我想要完成的事情。

4

1 回答 1

2

您应该能够只包含 Helper 模块并直接在标签定义中使用帮助程序,就像这样。

module CustomTags
  include Radiant::Taggable
  include ActionView::Helpers::TagHelper
  include ActionView::Helpers::AssetTagHelper

  tag "my_tag" do |tag|
    javascript_include_tag :some_js_file
  end
end
于 2011-07-14T20:30:37.833 回答