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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/amazon-web-services/12.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
Ruby on rails 在rails 3.2中使用ajax更新不同的div_Ruby On Rails_Ruby On Rails 3_Ruby On Rails 3.1_Ruby On Rails 3.2 - Fatal编程技术网

Ruby on rails 在rails 3.2中使用ajax更新不同的div

Ruby on rails 在rails 3.2中使用ajax更新不同的div,ruby-on-rails,ruby-on-rails-3,ruby-on-rails-3.1,ruby-on-rails-3.2,Ruby On Rails,Ruby On Rails 3,Ruby On Rails 3.1,Ruby On Rails 3.2,我最近开始使用Rails 3.2,但在尝试在我的应用程序中实现一些ajax功能时遇到了困难。我完全跟着这个铁路司机http://railscasts.com/episodes/240-search-sort-paginate-with-ajax. 除此之外,我还想为每个产品实现一个短名单按钮,让用户列出产品并将其存储在会话中。我还希望在同一页面上显示一个短名单产品的小列表,需要进行ajax更新 我想知道最好的方法是什么。我目前实现了带有远程标记的link_to按钮和一个helper函数,用于将链

我最近开始使用Rails 3.2,但在尝试在我的应用程序中实现一些ajax功能时遇到了困难。我完全跟着这个铁路司机http://railscasts.com/episodes/240-search-sort-paginate-with-ajax. 除此之外,我还想为每个产品实现一个短名单按钮,让用户列出产品并将其存储在会话中。我还希望在同一页面上显示一个短名单产品的小列表,需要进行ajax更新

我想知道最好的方法是什么。我目前实现了带有远程标记的link_to按钮和一个helper函数,用于将链接更改为shortlist/unshartlist。我还使用了一个条件div来根据短名单的长度显示短名单。然而,问题是每当我列出短名单时,products表的顺序也会重置

以下是我的代码片段:-

应用程序\u helper.rb

products/index.html.erb

我认为,问题是因为重定向到,但我不知道如何避免这种情况而不损害功能。我是不是走错了路。这是实现这一点的好方法吗。任何建议

谢谢,
Amit

您应该在短名单方法中使用

respond_to do |format|
  format.html {redirect_to products_path }#shortlist.html.erb
  format.js #shortlist.js.erb
 end
并将java脚本写入shortlist.js.erb文件


对unshortlist也要这样做。

我同意Sush的观点,您没有响应来自link_to的ajax请求

在控制器的短名单方法中,响应ajax请求

按照Rails中的约定,format.js将使用与方法相同的名称执行js.erb文件

在shortlist.js.erb中,您可以编写如下内容:


这似乎不起作用。当然,应该有javascript代码来操作html。如果您想执行特定于ajax请求的任何其他处理,也可以这样做;您可以使用.js{rails code here}block格式编写代码。然而,它仍然不起作用。仅当我将format.js请求重定向到products\u路径时,它才起作用。或者在我重新加载页面时不使用它。您在js.erb文件中写入了什么?$'shortlist'.html''页面/shortlist_字段'>'
<div id="product">
 <% @products.each do |product| %>
<tr>....
<td><%= shortlist_unshortlist_link(product.id.to_s) %></td>
</table>
</div>
<div class="shortlist"><%= shortlist_div() %> </div>
def shortlist
 user_session.add_to_shortlist(params[:id])
 redirect_to products_path
end

def unshortlist
 user_session.remove_from_shortlist(params[:id])
 redirect_to products_path
end
respond_to do |format|
  format.html {redirect_to products_path }#shortlist.html.erb
  format.js #shortlist.js.erb
 end
respond_to do |format|
  format.html {redirect_to products_path }
  format.js 
end
$('#shortlist').html('<%= escape_javascript(render "pages/shortlist_fields")%>');
respond_to do |format|
  format.html {redirect_to products_path }
  format.js {render :action=>'shortlist'}
end