【发布时间】:2014-03-18 03:49:35
【问题描述】:
所以我有一个带有字段按钮和表格的表单。我用数据填写字段,然后按添加按钮。然后在我的表中出现一个新行,无需重新加载页面----这就是它的样子
如何做到这一点?下面的一些代码。我做错了什么?
这是控制器的一部分
def create
@employee = Employee.new(employee_params)
@employee.save
respond_to do |format|
format.html{redirect_to root_url}
format.js
end
end
表格的一部分是这样包裹的
<table id="empl-table">
<%= render "employees"%>
</table>
<tr>
<th>FirstName</th>
<th>LastName</th>
<th>Position</th>
<th>Salary</th>
<th>#</th>
</tr>
<% @employees.each do |employee|%>
<tr>
<td><%=employee.first_name%></td>
<td><%=employee.last_name%></td>
<td><%=employee.position%></td>
<td><%=employee.salary%></td>
<td><%= link_to 'Destroy', employee, remote: true, method: :delete%></td>
</tr>
<% end %>
_create.js
$("#new_emp").click(function(){
$("#empl-table").append("
<tr>
<td><%=employee.first_name%></td>
<td><%=employee.last_name%></td>
<td><%=employee.position%></td>
<td><%=employee.salary%></td>
<td><%= link_to 'Destroy', employee, remote: true, method: :delete%></td>
<tr>
");
});
upd1(_form.html.erb)
<%= form_for :employee, remote: true, url: employees_path do |f|%>
<div class="row">
<div class="small-3 columns">
<%= f.label :first_name%>
<%= f.text_field :first_name%>
</div>
</div>
<div class="row">
<div class="small-3 columns">
<%= f.label :last_name%>
<%= f.text_field :last_name%>
</div>
</div>
<div class="row">
<div class="small-3 columns">
<%= f.label :position%>
<%= f.text_field :position%>
</div>
</div>
<div class="row">
<div class="small-3 columns">
<%= f.label :salary%>
<%= f.text_field :salary%>
</div>
</div>
<%= f.submit 'Add', id: "new_emp",class: 'button radius'%>
<% end %>
upd2
我以前将它作为单元格内容(我的意思是我在文本中引用的字符串)每个字段(employee.name,employee.position....),但现在什么都没有发生
【问题讨论】:
-
可能是重复的。你可能会喜欢这个 [post] [1] [1]:stackoverflow.com/questions/6896983/….
-
你能澄清一下这段代码现在发生了什么吗?它会抛出错误吗?如果它没有抛出错误,也没有添加新行,那么您拥有的 javascript 就不会被评估。
-
@andHapp 更新话题
标签: jquery ruby-on-rails ruby