【发布时间】:2014-06-24 15:45:19
【问题描述】:
在我的 index.html.erb 文件中,我有:
<table>
<thead>
<tr>
<th>Title</th>
<th>Note</th>
<th colspan="2"></th>
</tr>
</thead>
<tbody id="avail-courses">
<%= render partial: 'avail_course', collection: @courses %>
</tbody>
</table>
基本上呈现部分 _avail_course.html.erb。
我正在尝试通过 ajax 调用进行此渲染,因此我将我的 javascript 文件enroll.js.erb 设置为:
$('#avail-courses').html("<%= escape_javascript render partial: 'avail_course', collection: @courses %>");
ajax 调用成功,但渲染没有返回任何内容,从而使我的 id 为 #avail-courses 的表为空白。
我在这里遗漏了什么吗?我怀疑,在js文件中,我不能直接传入一个集合。
为了完整起见,我的部分 _avail_course.html.erb 是:
<tr>
<td><%= avail_course.title %></td>
<td><%= avail_course.note %></td>
<td><%= link_to 'Info', 'course_list/info/' + avail_course.id.to_s %></td>
<td><%= button_to 'Enroll', 'course_list/enroll/' + avail_course.id.to_s, method: :put, remote: true %></td>
</tr>
感谢任何输入。
【问题讨论】:
-
如果我没记错,那么您错过了拼写
_avail_courses.html.erb但您正在渲染avail_course。 -
拼错了。但是我的代码中的文件名是正确的。感谢您指出这一点。
标签: javascript ajax ruby-on-rails-4 renderpartial