I'll say right off the bat that I'm a complete beginner to Rails, so this might be something obvious that I'm missing.
I have a web form for editing an unordered (<ul>
) list of questions. Each question is an item in the list. Each question has a text field for the question text and a dropdown to indicate what type of question it is (multiple choice, true/false, etc).
I want to also give the user the ability to add new questions, by clicking a link and having a new text box and dropdown appear, as items on the list.
I haven't found a really clean way to do that, so I'm sort of piecing it together from various tutorials. Is there a better approach I'm missing?
Here's the code for edit.html.erb:
<h1>Editing Survey: <%= "#{@survey.name}" -%></h1>
<% form_for @survey, :url => {:action => 'update', :id => params[:id]} do |survey_form| %>
<h2>Title: <%= survey_form.text_field :name %></h2>
<ol class='question_list'>
<% for @question in @survey.questions %>
<% fields_for "question[]" do |f| %>
<li>
<%= f.text_field :q_text %><br>
<%= select(:question, :q_type, { "True/False" => "T", "Multiple Choice" => "M"}) %><br>
</li>
<% end %>
<% end %>
</ol>
<%=
link_to_function "Add another question",
update_page { |page|
page.insert_html :bottom,
'blank_question',
(render :partial => 'blank_question', :object => @question )
}
%>
<%= submit_tag "Update" %><br>
<% end %> <% "End of form_for" %>
<%= link_to "Back", {:action => 'list' } %>
I created another erb file called _blank_question.erb, which is the elements I would like to have included when they click on the "Add another question" link:
<li>
<%= text_field :question, :id, :size => 25 %><br>
<%= select(:question, :q_type, { "True/False" => "T", "Multiple Choice" => "M"}) %><br>
</li>
The problem is that I get the following error in a popup when I click on the "Add another question" link.
RJS error:
TypeError: Object function Element() { [native code] } has no method 'insert'[native code] } has no method 'insert'
Quite a bit of googling hasn't turned up any helpful results. Any idea where I'm going wrong here?