【问题标题】:if statement in js.erb file based on param_name will_paginate (Ajax call)js.erb 文件中基于 param_name will_paginate 的 if 语句(Ajax 调用)
【发布时间】:2012-12-10 10:41:54
【问题描述】:

我正在尝试使用 will_paginate Gem 在同一页面(同一控制器操作)上进行多个 Ajax 调用。到目前为止,我已经为每个实例变量提供了自己的 param_name,这样它们就不会使用 :page 的默认参数

示例控制器

@meats= Recipe.meat_recipes.paginate(:page => params[:meat_recipes], :per_page => 1)
@vegrecipes = Recipe.veg_recipes.paginate(:page => params[:veg_recipes], :per_page => 1)
@desserts = Recipe.dessert_recipes.paginate(:page => params[:dessert_recipes], :per_page => 1)

然后在视图中

 <%= will_paginate @meats,  :param_name => 'meat_recipes'  %> (repeat this for individual instance variables)

所以这部分很好。我正在做的是在像这样发出 AJAX 请求时加载部分

.js.erb 文件

$('#meatRecipes').html('<%= escape_javascript(render partial: 'meatrecipes') %>');
$.setAjaxPagination();

所以目前我的肉实例变量工作正常。那么如何根据 params 进行 if 语句呢?

这个例子我试过了

<% if @meats %>
$('#meatRecipes').html('<%= escape_javascript(render partial: 'meatrecipes') %>');
$.setAjaxPagination();
<% end %>

<% if @vegrecipes %>
$('#vegRecipes').html('<%= escape_javascript(render partial: 'vegrecipes') %>');
$.setAjaxPagination();
<% end %>

也试过了

<% if @meats[:param_name] == 'meat_recipes' %>
$('#meatRecipes').html('<%= escape_javascript(render partial: 'meatrecipes') %>');
$.setAjaxPagination();
<% end %>

<% if @vegrecipes[:param_name == 'veg_recipes' %>
$('#vegRecipes').html('<%= escape_javascript(render partial: 'vegrecipes') %>');
$.setAjaxPagination();
<% end %>

但这不起作用。所以我想我的问题是我用什么作为我的论据来确保为特定的分页部分调用正确的部分。

希望我已经正确解释了这一点

【问题讨论】:

    标签: ruby-on-rails ajax ruby-on-rails-3 jquery will-paginate


    【解决方案1】:

    好的,所以我需要将此解决方案归功于@karl,但这有效.. 除非有人有更好的方法?

     <% if params[:meat_recipes] %>
     $('#meatRecipes').html('<%= escape_javascript(render partial: 'meatrecipes') %>');
     $.setAjaxPagination();
     <% end %>
    
     <% if  params[:veg_recipes] %>
     $('#vegRecipes').html('<%= escape_javascript(render partial: 'vegeterianrecipes') %>');
     $.setAjaxPagination();
     <% end %>
    
     <% if  params[:dessert_recipes] %>
     $('#dessertRecipes').html('<%= escape_javascript(render partial: 'dessertrecipes') %>');
     $.setAjaxPagination();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-02-14
      • 1970-01-01
      • 1970-01-01
      • 2020-12-29
      • 2023-01-12
      • 2021-11-30
      • 1970-01-01
      相关资源
      最近更新 更多