Ich bin triying, um eine komplexe Form in Ruby On Rails zu machen. Es sollte in Gruppen unterteilt werden, und jede Gruppe sollte Textfelder haben. Ich möchte, dass Gruppen und Textfelder dynamisch hinzugefügt und entfernt werden.page.insert_html fügt html mit link_to_function (rails) nicht ein
Ich habe es bereits geschafft, Textfelder hinzuzufügen und zu entfernen, aber ich bin völlig unfähig dasselbe mit den Gruppen zu machen.
Dies ist der Aufruf der Funktion:
<%= link_to_function 'Add group' do |page|
page.insert_html :bottom, :groups, :partial => 'group'
end %>
So soll es die teilweise den Namen „Gruppe“ zu den „Gruppen“ div direkt hinzufügen? naja, tut es nicht. Dies ist das Teil:
<div id="group" class='elements'>
<p>
<g_title>Text Group</g_title>
<add><%= add_text_link 'Add a text field'%></add>
<remove><%= link_to_function 'Remove text group', "this.up('.elements').remove()" %></remove>
</p>
<%= render :partial => 'text' %>
</div>
Wenn ich auf der „Neuen Gruppe“ Link klicken, Firebug mir sagen, es ist ein Fehler, wie einige Javascript-Code ist nicht gültig (der Fehler „missing} nach Eigenschaftsliste“). Aber, wenn ich die vierte Zeile im Teilcode entferne (die mit der Funktion "add_text_link"), funktioniert es perfekt! und diese Funktion funktioniert perfekt.
Also, irgendeine Idee?
Danke für Ihre Hilfe!
EDIT: Dies ist die generierten Quellcode:
<form action="/collect/text" method="post"><div style="margin:0;padding:0"><input name="authenticity_token" type="hidden" value="b2zqh1op9oHbjQ33mMAWEhZcEqHoYhUIMV0uPH8F2ms=" /></div>
<div id= 'main_elements' class='elements'>
<p><g_title>Main elements</g_title></p>
<p><input id="Title" name="Title" size="50" type="text" />
<label class="h0" for="Title">Title</label> <options>(mandatory)</options></p>
<p><input id="Subtitle" name="Subtitle" size="50" type="text" />
<label class="h0" for="Subtitle">Subtitle</label> <options>(optional)</options></p>
</div>
<div id='groups' class="groups">
<p><add><a href="#" onclick="try {
Element.insert("groups", { bottom: "<p></p>\n<div id=\"group\" class='elements'>\n\t<p>\n\t\t<g_title>Text Group</g_title>\n\t\t<add><a href=\"#\" onclick=\"try {\nElement.insert("group", { bottom: "\\n<div class='text'>\\n\\t<p>\\n\\t\\tText <input id=\\"content\\" name=\\"content\\" size=\\"50\\" type=\\"text\\" /> Importance <select id=\\"t1\\" name=\\"t1\\"><option value=\\"notes\\">Notes</option>\\n<option value=\\"highlighted\\">Highlighted text</option>\\n<option value=\\"normal\\">Normal text</option></select>\\n\\t\\t<remove><a href=\\"#\\" onclick=\\"this.up('.text').remove(); return false;\\">Remove text field</a></remove>\\n\\t</p>\\n</div>" });\nnew Effect.Highlight("group",{duration:0.3, startcolor:'#34d85e'});\n} catch (e) { alert('RJS error:\\n\\n' + e.toString()); alert('Element.insert(\\"group\\", { bottom: \\"\\\\n<div class=\\'text\\'>\\\\n\\\\t<p>\\\\n\\\\t\\\\tText <input id=\\\\\\"content\\\\\\" name=\\\\\\"content\\\\\\" size=\\\\\\"50\\\\\\" type=\\\\\\"text\\\\\\" /> Importance <select id=\\\\\\"t1\\\\\\" name=\\\\\\"t1\\\\\\"><option value=\\\\\\"notes\\\\\\">Notes</option>\\\\n<option value=\\\\\\"highlighted\\\\\\">Highlighted text</option>\\\\n<option value=\\\\\\"normal\\\\\\">Normal text</option></select>\\\\n\\\\t\\\\t<remove><a href=\\\\\\"#\\\\\\" onclick=\\\\\\"this.up(\\'.text\\').remove(); return false;\\\\\\">Remove text field</a></remove>\\\\n\\\\t</p>\\\\n</div>\\" });\\nnew Effect.Highlight(\\"group\\",{duration:0.3, startcolor:\\'#34d85e\\'});'); throw e }; return false;\">Add a text field</a></add>\n\t\t<remove><a href=\"#\" onclick=\"this.up('.elements').remove(); return false;\">Remove text group</a></remove>\n\t</p>\n\t\n\t\n<div class='text'>\n\t<p>\n\t\tText <input id=\"content\" name=\"content\" size=\"50\" type=\"text\" /> Importance <select id=\"t1\" name=\"t1\"><option value=\"notes\">Notes</option>\n<option value=\"highlighted\">Highlighted text</option>\n<option value=\"normal\">Normal text</option></select>\n\t\t<remove><a href=\"#\" onclick=\"this.up('.text').remove(); return false;\">Remove text field</a></remove>\n\t</p>\n</div>\n</div>" });
new Effect.Highlight("group",{duration:0.3, startcolor:'#34d85e'});
} catch (e) { alert('RJS error:\n\n' + e.toString()); alert('Element.insert(\"groups\", { bottom: \"<p></p>\\n<div id=\\\"group\\\" class=\'elements\'>\\n\\t<p>\\n\\t\\t<g_title>Text Group</g_title>\\n\\t\\t<add><a href=\\\"#\\\" onclick=\\\"try {\\nElement.insert("group", { bottom: "\\\\n<div class=\'text\'>\\\\n\\\\t<p>\\\\n\\\\t\\\\tText <input id=\\\\"content\\\\" name=\\\\"content\\\\" size=\\\\"50\\\\" type=\\\\"text\\\\" /> Importance <select id=\\\\"t1\\\\" name=\\\\"t1\\\\"><option value=\\\\"notes\\\\">Notes</option>\\\\n<option value=\\\\"highlighted\\\\">Highlighted text</option>\\\\n<option value=\\\\"normal\\\\">Normal text</option></select>\\\\n\\\\t\\\\t<remove><a href=\\\\"#\\\\" onclick=\\\\"this.up(\'.text\').remove(); return false;\\\\">Remove text field</a></remove>\\\\n\\\\t</p>\\\\n</div>" });\\nnew Effect.Highlight("group",{duration:0.3, startcolor:\'#34d85e\'});\\n} catch (e) { alert(\'RJS error:\\\\n\\\\n\' + e.toString()); alert(\'Element.insert(\\\\"group\\\\", { bottom: \\\\"\\\\\\\\n<div class=\\\\\'text\\\\\'>\\\\\\\\n\\\\\\\\t<p>\\\\\\\\n\\\\\\\\t\\\\\\\\tText <input id=\\\\\\\\\\\\"content\\\\\\\\\\\\" name=\\\\\\\\\\\\"content\\\\\\\\\\\\" size=\\\\\\\\\\\\"50\\\\\\\\\\\\" type=\\\\\\\\\\\\"text\\\\\\\\\\\\" /> Importance <select id=\\\\\\\\\\\\"t1\\\\\\\\\\\\" name=\\\\\\\\\\\\"t1\\\\\\\\\\\\"><option value=\\\\\\\\\\\\"notes\\\\\\\\\\\\">Notes</option>\\\\\\\\n<option value=\\\\\\\\\\\\"highlighted\\\\\\\\\\\\">Highlighted text</option>\\\\\\\\n<option value=\\\\\\\\\\\\"normal\\\\\\\\\\\\">Normal text</option></select>\\\\\\\\n\\\\\\\\t\\\\\\\\t<remove><a href=\\\\\\\\\\\\"#\\\\\\\\\\\\" onclick=\\\\\\\\\\\\"this.up(\\\\\'.text\\\\\').remove(); return false;\\\\\\\\\\\\">Remove text field</a></remove>\\\\\\\\n\\\\\\\\t</p>\\\\\\\\n</div>\\\\" });\\\\nnew Effect.Highlight(\\\\"group\\\\",{duration:0.3, startcolor:\\\\\'#34d85e\\\\\'});\'); throw e }; return false;\\\">Add a text field</a></add>\\n\\t\\t<remove><a href=\\\"#\\\" onclick=\\\"this.up(\'.elements\').remove(); return false;\\\">Remove text group</a></remove>\\n\\t</p>\\n\\t\\n\\t\\n<div class=\'text\'>\\n\\t<p>\\n\\t\\tText <input id=\\\"content\\\" name=\\\"content\\\" size=\\\"50\\\" type=\\\"text\\\" /> Importance <select id=\\\"t1\\\" name=\\\"t1\\\"><option value=\\\"notes\\\">Notes</option>\\n<option value=\\\"highlighted\\\">Highlighted text</option>\\n<option value=\\\"normal\\\">Normal text</option></select>\\n\\t\\t<remove><a href=\\\"#\\\" onclick=\\\"this.up(\'.text\').remove(); return false;\\\">Remove text field</a></remove>\\n\\t</p>\\n</div>\\n</div>\" });\nnew Effect.Highlight(\"group\",{duration:0.3, startcolor:\'#34d85e\'});'); throw e }; return false;">Add new group</a></add></p>
<p></p>
<div id="group" class='elements'>
<p>
<g_title>Text Group</g_title>
<add><a href="#" onclick="try {
Element.insert("group", { bottom: "\n<div class='text'>\n\t<p>\n\t\tText <input id=\"content\" name=\"content\" size=\"50\" type=\"text\" /> Importance <select id=\"t1\" name=\"t1\"><option value=\"notes\">Notes</option>\n<option value=\"highlighted\">Highlighted text</option>\n<option value=\"normal\">Normal text</option></select>\n\t\t<remove><a href=\"#\" onclick=\"this.up('.text').remove(); return false;\">Remove text field</a></remove>\n\t</p>\n</div>" });
new Effect.Highlight("group",{duration:0.3, startcolor:'#34d85e'});
} catch (e) { alert('RJS error:\n\n' + e.toString()); alert('Element.insert(\"group\", { bottom: \"\\n<div class=\'text\'>\\n\\t<p>\\n\\t\\tText <input id=\\\"content\\\" name=\\\"content\\\" size=\\\"50\\\" type=\\\"text\\\" /> Importance <select id=\\\"t1\\\" name=\\\"t1\\\"><option value=\\\"notes\\\">Notes</option>\\n<option value=\\\"highlighted\\\">Highlighted text</option>\\n<option value=\\\"normal\\\">Normal text</option></select>\\n\\t\\t<remove><a href=\\\"#\\\" onclick=\\\"this.up(\'.text\').remove(); return false;\\\">Remove text field</a></remove>\\n\\t</p>\\n</div>\" });\nnew Effect.Highlight(\"group\",{duration:0.3, startcolor:\'#34d85e\'});'); throw e }; return false;">Add a text field</a></add>
<remove><a href="#" onclick="this.up('.elements').remove(); return false;">Remove text group</a></remove>
</p>
<div class='text'>
<p>
Text <input id="content" name="content" size="50" type="text" /> Importance <select id="t1" name="t1"><option value="notes">Notes</option>
<option value="highlighted">Highlighted text</option>
<option value="normal">Normal text</option></select>
<remove><a href="#" onclick="this.up('.text').remove(); return false;">Remove text field</a></remove>
</p>
</div>
</div>
</div>
<div class="button">
<p><next><a href="#" onclick="$(this).up('form').submit(); return false;">Next Step</a></next></p>
</div>
</form>
Aber auf diese Weise kann ich nicht soviel hinzufügen wie ich will, kann ich? Ich meine, es gibt nur einen versteckten, und ich möchte, dass er so viele hinzufügen kann, wie der Benutzer möchte. –