Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ajax:在rails3+;formtastic_Ajax_Ruby On Rails 3_Formtastic - Fatal编程技术网

Ajax:在rails3+;formtastic

Ajax:在rails3+;formtastic,ajax,ruby-on-rails-3,formtastic,Ajax,Ruby On Rails 3,Formtastic,我已经用ajax表单在示例formtastic中成功地尝试了ajax保存 新值将添加到数据库中。但问题在于,我通过ajax保存后,立即从数据库中检索列表 怎么做 一旦我添加了一条新记录,我希望我的显示列表得到更新。添加新记录和列出数据库数据的选项都在同一页中 这是我的索引页。通过脚手架创建的控制器和所有其他 <h1>Listing samples</h1> <table> <tr> <th><%=t :Name%>&l

我已经用ajax表单在示例formtastic中成功地尝试了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 %>
列出示例
:delete,:data=>{:confirm=>'你确定吗?}%>








样本路径:remote=>true do | f |%> :输入%>
通过ajax保存时,可能需要更改控制器的响应方式,有点像这样:

def create
  # stuff
  respond_to do |format|
    format.js
  end
end
当您这样做时,rails希望您已经创建了一个名为
create.js.erb
的文件,您可以在其中操作视图的数据(将新内容附加到表中,使用新对象列表呈现一个全新的部分,等等):

$('your#u list').html(
""
);

$('your#u list')。追加(
""
);

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

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

def create
  # stuff
  respond_to do |format|
    format.js
  end
end
当您这样做时,rails希望您已经创建了一个名为
create.js.erb
的文件,您可以在其中操作视图的数据(将新内容附加到表中,使用新对象列表呈现一个全新的部分,等等):

$('your#u list').html(
""
);

$('your#u list')。追加(
""
);

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

我使用这些指南执行CRUD操作,效果非常好


我使用这些指南进行了CRUD操作,效果非常好

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