I'm trying to append a form to a div tag
$('#courses_ajax').html('<h1>Materie: <%= @course.name %></h1>');
msg = "<form accept-charset='UTF-8' action='/add_by_course' class='form-horizontal' data-remote='true' id='add_by_courses_id' method='post'>";
msg += "<table class='table table-condensed table-bordered'>";
msg +="<th>Nume Elev</th><th>Nota</th><th>Absente</th>";
<% @students.each do |student|%>
msg +="<tr><td><%= student.name%></td>";
msg +="<td><input class='span2' id='course_grade' name='grade[student_id]' size='30' type='number' min=1 max=10 /></td>";
msg +="<td><input class='span2' id='course_absence' name='absence[student_id]' size='30' type='datetime' /></td>";
msg +="</tr>";
<%end%>
msg +="</table><input class='btn-primary' name='mit' type='submit' value='Salveaza' />";
msg +="</form>";
$('#courses_ajax').append(msg);
All the helper methods don't seem to work in js.erb files , is there a better more cleaner way to do this , maybe I'm missing something .
I'm trying to append a form to a div tag
$('#courses_ajax').html('<h1>Materie: <%= @course.name %></h1>');
msg = "<form accept-charset='UTF-8' action='/add_by_course' class='form-horizontal' data-remote='true' id='add_by_courses_id' method='post'>";
msg += "<table class='table table-condensed table-bordered'>";
msg +="<th>Nume Elev</th><th>Nota</th><th>Absente</th>";
<% @students.each do |student|%>
msg +="<tr><td><%= student.name%></td>";
msg +="<td><input class='span2' id='course_grade' name='grade[student_id]' size='30' type='number' min=1 max=10 /></td>";
msg +="<td><input class='span2' id='course_absence' name='absence[student_id]' size='30' type='datetime' /></td>";
msg +="</tr>";
<%end%>
msg +="</table><input class='btn-primary' name='mit' type='submit' value='Salveaza' />";
msg +="</form>";
$('#courses_ajax').append(msg);
All the helper methods don't seem to work in js.erb files , is there a better more cleaner way to do this , maybe I'm missing something .
Share Improve this question asked Feb 12, 2012 at 13:49 lescelesce 6,3245 gold badges31 silver badges35 bronze badges2 Answers
Reset to default 6It seems that it is better to use partials here. Create a partial and put html and helpers there. Then put into you js.erb file:
$('#courses_ajax').append("<%= escape_javascript(
render :partial => "your_partial", :object => your_varable,
:locals => {:your_other_var => value, ...} %>");
Rendering a partial with the contents in pure html solves it BUT I remend you let rails render it like:
<%= form_for(@xxxx) ... %> ... "html here" ... <%end%>
as that will protect you from CSRF attacks generating an "authenticity_token".
It did not work for me to add more rails generated <%= %>
input fields, but with the <%= form ... %>
is good enough.