Kaminari Pagination与自定义路线和ajax

时间:2014-01-31 07:31:26

标签: ruby-on-rails ajax pagination kaminari

我正在尝试通过Kanimari gem中的远程调用更新分页div。由于这是通过ajax,我想将它发送到自定义控制器,所以我只更新一个div,而不是整个页面。不幸的是,当我点击下一个链接时,这是输出。

Started GET "/technologies/pages?page=2" for 127.0.0.1 at 2014-01-30 23:14:27 -0800
Processing by TechnologiesController#show as HTML
   Parameters: {"page"=>"2", "id"=>"pages"}
   User Load (0.4ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
   Technology Load (0.5ms)  SELECT "technologies".* FROM "technologies" WHERE  "technologies"."id" = $1 LIMIT 1  [["id", "pages"]]
Completed 404 Not Found in 5ms

ActiveRecord::RecordNotFound (Couldn't find Technology with id=pages):
    app/controllers/technologies_controller.rb:59:in `show'

我不明白为什么会发生这种情况。这是分页发生的部分,以及我的路线。

部分:

= paginate @technologies, params: {controller: :technologies, action: :pages}

路线

resources :technologies do
   collection do
      post :search
      get :pages
   end
   resources :comments
end

控制器方法

def pages
   technologies = query(params)

   per_page = 12

   #Paginate
   @technologies = technologies.page(params[:page]).per(per_page)

end

修改

发现还有另一种资源:routes.rb。

开头的技术

现在,当我点击Next时,这是输出。

Started GET "/technologies/pages?page=2" for 127.0.0.1 at 2014-01-30 23:57:11 -0800
Processing by TechnologiesController#pages as HTML
   Parameters: {"page"=>"2"}
   User Load (0.3ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1

为什么处理为html而不是js?

1 个答案:

答案 0 :(得分:0)

在ajax请求中,HTML表示服务器,因此在成功回调时,内容可以用响应替换,这是正确的。在你的控制器中,你需要渲染你的视图或部分没有布局的分页xhr请求。

示例:

respond_to do |format|
 format.html do 
   render :partial => 'pages', :layout => false and return if request.xhr?#if you have partial for paginated records  
   # for non xhr request 'pages' view will be rendered, or write here the name of view if it differs with action name (render '<view_name>')
 end
end

如果你没有部分,只需写下面的内容。

render :layout => false and return if request.xhr?