0

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='commit' 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 .

2 Answers 2

6

It 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, ...}  %>");
Sign up to request clarification or add additional context in comments.

Comments

2

Rendering a partial with the contents in pure html solves it BUT I recommend 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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.