【发布时间】:2015-01-28 18:24:16
【问题描述】:
我正在尝试在rails 中使用ajax 表单并在我的表单上使用Remote => True。
现在可以了,当我点击添加新主题时。会发生以下情况
1.form 部分将附加到正文中(其中包含表单) 2.Bootstrap Modal会显示在里面的表格 3.点击提交后,模态框消失,数据将被存储(成功)
我的问题是原来的 Index.html.erb 视图现在已经过时了,当数据插入我的数据库时,视图仍然显示旧数据。
通过 ajax 成功提交表单后,如何触发我的视图更新?
谢谢!!
我的 subject.rb 控制器
def new
@subject = Subject.new({:name => "Default"})
@subject_count= Subject.count + 1
end
def create
@subject = Subject.new(subject_params)
if @subject.save
flash[:toastr] = ["success",'Subject #{@subject.name} successfully created']
respond_to do |format|
format.html {redirect_to(:action => "index")}
format.js
end
else
@subject_count= Subject.count+1
render('new')
end
end
这是我的索引视图
<% @subjects.each do |subject| %>
<tr class="<%= cycle('odd', 'even') %>">
<td><%= subject.position %></td>
<td><%= subject.name %></td>
<td class="center"><%= status_tag(subject.visible) %></td>
<td class="center"><%= subject.pages.size %></td>
<td class="actions">
<%= link_to("Show", {:action => 'show', :id => subject.id}, :class => 'action show') %>
<%= link_to("Edit", {:action => 'edit', :id => subject.id}, :class => 'action edit') %>
<%= link_to("Delete", {:action => 'delete', :id => subject.id}, :class => 'action delete') %>
</td>
</tr>
<% end %>
我的 new.js.erb
$('body').append(' <%= j render(:partial => "form") %>')
$('#modalform').modal('show')
我的 create.js.erb
$('#modalform').modal('hide')
【问题讨论】:
标签: jquery ruby-on-rails erb