Ruby on rails Rails-will_paginate和ActionCable服务器广播冲突并导致错误(未定义的方法'total_pages';)
更新项目时,我正在使用ActionCable服务器广播。添加will_paginate后,现在会导致错误。当我从更新方法中删除广播时,它可以正常工作。如果包含,则在尝试更新时会出现以下错误: 产品中的命名错误#更新 #Ruby on rails Rails-will_paginate和ActionCable服务器广播冲突并导致错误(未定义的方法'total_pages';),ruby-on-rails,Ruby On Rails,更新项目时,我正在使用ActionCable服务器广播。添加will_paginate后,现在会导致错误。当我从更新方法中删除广播时,它可以正常工作。如果包含,则在尝试更新时会出现以下错误: 产品中的命名错误#更新 # 显示/Users/matt/rails/store/app/views/store/index.html.erb,其中第33行出现: 您需要先调用paginate,然后再调用will\u paginate: @products = Product.all.paginate(:pa
您需要先调用
paginate
,然后再调用will\u paginate
:
@products = Product.all.paginate(:page => params[:page], :per_page => 25)
ActionCable.server.broadcast 'products', html: render_to_string('store/index', layout: false)
请使用
stores\u controller\index
action@mxvx,作为一种必然结果,您只需广播更改后的产品,并仅当该产品当前可见时才让订阅者更新,这可能会对您有利。这样,您就完全避开了分页问题,广播的信息就更少了。
class ProductsController < ApplicationController
before_action :set_product, only: [:show, :edit, :update, :destroy]
...
def index
@search = Product.search(params[:q])
@products = @search.result
@products = @products.paginate(:page => params[:page], :per_page => 25)
end
def update
respond_to do |format|
if @product.update(product_params)
format.html { redirect_to @product, notice: 'Product was successfully updated.' }
format.json { render :show, status: :ok, location: @product }
@products = Product.all
ActionCable.server.broadcast 'products', html: render_to_string('store/index', layout: false)
else
format.html { render :edit }
format.json { render json: @product.errors, status: :unprocessable_entity }
end
end
end
...
end
ActionCable.server.broadcast 'products', html: render_to_string('store/index', layout: false)
@products = Product.all.paginate(:page => params[:page], :per_page => 25)
ActionCable.server.broadcast 'products', html: render_to_string('store/index', layout: false)