1

我是 ruby​​ on rails 的新手,我目前正在使用 cocoon gem 在 haml 中构建一个简单的嵌套表单。下面的代码是一个显示我的“部分”数据(部分是父项)的视图,如果用户单击“添加问题”按钮,该表格将嵌入到页面中。

当表单最初呈现时,我们看到了理想的情况。部分字段在屏幕上,问题标题在其下方,“add_question”按钮也出现在那里。

当我单击“add_question”时,页面上会出现 2 个问题集 - 在部分字段上方。我希望它们出现在部分字段下方,但我不知道该怎么做。没有特殊的 CSS 影响页面,这是一个简单的页面。

剖面视图页面在这里:

%h2 Sections
= semantic_form_for @section do |f|
  = f.inputs do
    = f.input :title
    = f.input :body, :class => "textareea", :rows => 40, :cols => 6 
    = f.input :image_file_name
    = f.check_box :good_opinion, :class => "checkbox"
    = f.label "Check me if you have a good opinion of this feature", :class => "checkbox" 
    %hr
    %h3 Questions
    #questions
    = f.semantic_fields_for :section.questions do |question|
      = render 'question_fields', :f => question
      .links
    = link_to_add_association 'add question', f, :questions
  %p 
  %input{type: 'submit', value: 'Submit'}

question_fields 表单部分在这里(.nested-fields css 样式当前为空):

.nested-fields
  = f.inputs do
    = f.input :question_text
    = f.input :answer_text
    = f.input :section_id
    = link_to_remove_association "remove question", f

知道我能做些什么来使这变得更好吗?

4

1 回答 1

1

link_to_add_association has these options:

  • name: the text to show in the link f: the form builder association: the name of the association (plural) of which a new instance needs to be added (symbol or string).

  • html_options: extra html-options (see link_to There are some special options, the first three allow to control the placement of the new link-data:

    • data-association-insertion-traversal : the jquery traversal method to allow node selection relative to the link. closest, next, children, etc. Default: absolute selection

    • data-association-insertion-node : the jquery selector of the node


Perhaps you should do something like this:

link_to_add_association 'add question', f, :questions, data: {"association-insertion-method" => "after"} 
于 2014-03-23T13:11:26.993 回答