2012-12-12 35 views
1

我已经成功地尝试了使用ajax格式在我的示例formtastic中保存ajax。Ajax:在rails3 + formtastic中检索数据

将新值添加到数据库中。但问题是我一旦通过ajax保存,就从数据库中检索列表。

怎么办?

只要我添加一条新记录,我希望我的显示列表更新。添加新记录和列出数据库数据的选项都在同一页

这是我的索引页。通过脚手架创建控制器和所有其他

<h1>Listing samples</h1> 

<table> 
    <tr> 
<th><%=t :Name%></th> 
    <th></th> 
    <th></th> 
    <th></th> 
    </tr> 

<% @samples.each do |sample| %> 
    <tr> 
    <td><%= sample.name %></td> 
    <td><%= link_to 'Show', sample %></td> 
    <td><%= link_to 'Edit', edit_sample_path(sample) %></td> 
    <td><%= link_to 'Destroy', sample, :method => :delete, :data => { :confirm => 'Are you sure?' } %></td> 
    </tr> 
<% end %> 
</table> 

<br /> 

<%= link_to 'New Sample', new_sample_path %> 

<br /><br /><br /><br /><br /> 
<%= semantic_form_for @sample1,:url => samples_path, :remote => true do |f| %> 
    <%= f.inputs do %> 

    <%= f.input :name %> 
    <% end %> 
    <%= f.actions do %> 
    <%= f.action :submit, :as => :input %> 
    <% end %> 
<% end %> 

回答

4

当你通过AJAX保存,你可能要改变你的控制器响应的方式,有点像这样:

def create 
    # stuff 
    respond_to do |format| 
    format.js 
    end 
end 

执行此操作时,轨道会希望你已经创建了一个名为create.js.erb文件,在其中你可以操纵你的观点的数据(追加新的内容到你的表,呈现一个全新的部分与新对象列表等):

$('#your_list').html(
    "<%= escape_javascript(render('your_table_partial')) %>" 
); 

或者

$('#your_list').append(
    "<%= escape_javascript(render('your_item_of_the_table_partial')) %>" 
); 

这些只是例子,我不知道你的代码足够写正确的代码给你,但你确实可以把这些作为你工作的基础。