【问题标题】:Ajax : data retrieving in rails3 + formtasticAjax:在rails3 + formtastic中检索数据
【发布时间】:2012-12-12 09:02:14
【问题描述】:

我已成功尝试使用 ajax 表单在我的示例表单中保存 ajax。

新值被添加到数据库中。但问题在于我通过 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 %>

【问题讨论】:

    标签: ajax ruby-on-rails-3 formtastic


    【解决方案1】:

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

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

    当你这样做时,rails 会期望你创建了一个名为 create.js.erb 的文件,你可以在其中操作视图的数据(将新内容附加到表中,使用新对象列表呈现一个全新的部分等):

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

    或者

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

    这些只是示例,我对您的代码了解得不够多,无法为您编写正确的代码,但您确实可以将这些作为您工作的基础。

    【讨论】:

      【解决方案2】:

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-02-17
        • 1970-01-01
        • 2012-02-23
        • 1970-01-01
        • 2012-07-27
        • 1970-01-01
        相关资源
        最近更新 更多