Thank you in advance.
Although I have many years of experience in general, including cross-
platform processing, I am not an HTML/Javascript programmer. As a
result, I do not have certain specific baseline skills and/or knowledge
that are presumed in the Rails and Ajax documentation.
I am experienced with DOM manipulations, so the bare mechanics of
manipulating the browser GUI via Javascript and the DOM tree are self
evident. However, this does not get the input data back to the server
side of the application.
The immediate question that have is this: When I dynamically add rows
of inputs to a table, how do get the values entered by the user into
those rows back to the rails application?
The specific example is a screen for editing a question with its list of
answers as a sovereign application. Both code snips are from
views/question/_form.rhtml.
I realize that I do not have the server side of the interface
represented from controllers/question_controller.rb. In part, this is
because I have a gap in my knowledge about what the HTTP post will
provide back to the ruby app - it is presumed to be self evident in the
books I have available.
The following javascript is the code that inserts the new row into the
table in the browser context (not the backing RDBMS):
function insertAnswer () {
table = document.getElementById (“answersTable”);
body = document.getElementById (“answersBody”);
row = document.createElement(“tr”);
row.setAttribute(“id”,"");
// need to build a Ruby friendly naming scheme for
// elements so they can be passed as parameters
data = document.createElement(“td”);
row.appendChild(data);
data = document.createElement(“td”);
row.appendChild(data);
data = document.createElement(“td”);
node = document.createElement(“input”)
node.setAttribute (“type”,“checkbox”);
data.appendChild(node);
row.appendChild(data);
data = document.createElement(“td”);
node = document.createElement(“input”)
node.setAttribute (“type”,“text”);
node.setAttribute (“size”,“3”);
data.appendChild(node);
row.appendChild(data);
data = document.createElement(“td”);
node = document.createElement(“input”)
node.setAttribute (“type”,“text”);
data.appendChild(node);
row.appendChild(data);
data = document.createElement(“td”);
btn = document.createElement(“button”)
btn.setAttribute (“name”,“linkPresentation”);
btn.setAttribute (“type”,“button”);
btn.setAttribute (“onClick”,“linkAnswerToPresentation”);
node = document.createElement(“img”);
node.setAttribute (“src”,"/images/file-document.png");
btn.appendChild(node);
data.appendChild(btn);
row.appendChild(data);
data = document.createElement(“td”);
node = document.createElement(“input”)
node.setAttribute (“type”,“text”);
data.appendChild(node);
row.appendChild(data);
data = document.createElement(“td”);
btn = document.createElement(“button”)
btn.setAttribute (“name”,“linkAudio”);
btn.setAttribute (“type”,“button”);
btn.setAttribute (“onClick”,“linkPresentationToAudio()”);
node = document.createElement(“img”);
node.setAttribute (“src”,"/images/file-audio.png");
btn.appendChild(node);
data.appendChild(btn);
row.appendChild(data);
data = document.createElement(“td”);
node = document.createElement(“input”)
node.setAttribute (“type”,“text”);
data.appendChild(node);
row.appendChild(data);
data = document.createElement(“td”);
btn = document.createElement(“button”)
btn.setAttribute (“name”,“linkArt”);
btn.setAttribute (“type”,“button”);
btn.setAttribute (“onClick”,“linkPresentationToArt()”);
node = document.createElement(“img”);
node.setAttribute (“src”,"/images/file-art.png");
btn.appendChild(node);
data.appendChild(btn);
row.appendChild(data);
data = document.createElement(“td”);
btn = document.createElement(“button”)
btn.setAttribute (“name”,“answer”);
btn.setAttribute(“id”,“delete_”+table.rows.length);
btn.setAttribute (“type”,“button”);
btn.setAttribute (“onClick”,“deleteAnswer(this)”);
node = document.createElement(“img”);
node.setAttribute (“src”,"/images/trashcan_empty.png");
btn.appendChild(node);
data.appendChild(btn);
row.appendChild(data);
body.appendChild(row);
}
The equivalent RHTML fragment that builds the table from previosly
stored data is:
<%
answers = Answers.find (:all, :conditions => "parent_id = " +
@question.id.to_s)
for answer in answers
presentation = Presentations.find_by_id
(answer.presentation_id.to_s);
-%>
<td><input name="loadText" type="img" value="file..."
src="/images/file-document.png" onClick=“linkPresentation()”/>
<% end %>