Question

I am trying to update a pagination div through the remote call in the Kanimari gem. Since this is going though ajax, I want to send it to a custom controller so I am only updating one div, instead of the entire page. Unfortunately, this is the output when I click the next link.

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'

I am not understanding why this would be happening. Here is the partial where the pagination is happening, as well as my routes.

Partial:

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

Routes

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

Controller Method

def pages
   technologies = query(params)

   per_page = 12

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

end

EDIT

Found out that there was another resources :technologies in the beginning of the routes.rb.

Now, when I click on Next, here is the output.

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

Why is it processing as html and not js?

Was it helpful?

Solution

In the ajax request HTML is being asked form server, So that in success callback the content can be replaced with the response and this is correct. In your controller you need to render your view or partial without layout for pagination xhr request.

Example:

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

If you dont have partial simply write as below.

render :layout => false and return if request.xhr?
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top