will_paginate causes ActiveRecord::RecordNotFound in show - ruby-on-rails

In our rails 3.2.8 app, the will_paginate causes ActiveRecord::RecordNotFound error when clicking next page or another page after page 1. Here is the error:
ActiveRecord::RecordNotFound in LeaseBookingsController#show
Couldn't find LeaseBooking with id=search_results
Parameters:
{"page"=>"4",
"id"=>"search_results"}
The search_results is a custom action in controller:
def search_results
#lease_booking = LeaseBooking.new(params[:lease_booking], :as => :roles_search)
#lease_bookings = #lease_booking.find_lease_bookings.paginate(:per_page => 40, :page => params[:page])
end
find_lease_bookings is a method in models to retrieve the lease_booking record. The error is only with will_paginate. In gemfile there is:
gem 'will_paginate', '~> 3.0'
Any solution to solve the paging issue? Thanks.

I faced a similar issue today and wasted hell lot of time. And here is the reason.
"If your form's action is POST (or any other HTTP method) then the search term won't be applied to subsequent page links. In other words, when you follow the "Next page" or any other page link, the search parameter(s) would be lost."
Check out this link.
https://github.com/mislav/will_paginate/wiki/Simple-search
So the solution is use GET in place of POST.

Related

will_paginate giving blank pages

I'm use will_paginate gem in my Rails application.
controller code:
def index
#posts = Guestbook.order('id DESC').page(params[:page]).per_page(10)
end
view code (used foundation gem):
<%= foundation_paginate #posts %>
Everything works fine, but if you type in the address bar room for a page, a blank page is displayed (no error messages or redirect to the first page). Tell me how to fix it, please.
P.S. I'm sorry for my bad english
you have to set will_paginate to use an array.
In config/initializers/
create a new file called will_paginate_extensions.rb
and add this:
require 'will_paginate/array'
I also would recommend not to use that foundation gem you are using for will_paginate, It can't receive parameters. I ran into that problem with that gem so i made one that can receive parameters
https://github.com/acrogenesis/will_paginate-foundation

Active_admin NoMethodError

I am using active_admin to generate an admin page on my rails app. I have had it for like a week, but suddenly today when I click the link emoticon which is a resource for the admin, I get this error :
NoMethodError in Admin::EmoticonsController#index
undefined method `per' for #
here is my emoticon controller :
def index
#emoticons = Emoticon.search(params[:search]).order(sort_column + ' ' + sort_direction).paginate(:per_page => 10, :page => params[:page])
end
I don't know, if I accidentally deleted one of active_admin file. But I recheck with the Railscast folder. And I think everything is fine. Anyone has any idea where the error could come from?
Please let me know if you need any other files.
Thanks.
Have you seen this? Assuming you are working with will_paginate.

Rails link_to with remote: true processing html instead of js after page refresh

I have a search page in my app, where there is an ajax search form. The search form works properly, passing parameters to the model to filter the search, and the model returning a collection of results. Upon search submit, #results are rendered on the page. Each #result then has a link to an action on it, like:
<%=link_to "Message", message_user_path(:id => user.id), :remote => true%>
Where this action in the controller is:
respond_to :js, :html
def message
#user_id = params[:id]
#user = User.find_by_id(#user_id)
respond_to do |format|
format.html
format.js
end
end
and this responds with message.js.erb, which triggers a messaging panel to pop up with a message to the user. All of this is working correctly, checking the log I see the correct get request sent, and the correct format being processed:
Started GET "/users/3/message"
Processing by UsersController#message as JS
However, if I refresh the page and try to click the same link that was working before, I get the error Template is Missing. Checking the log, I see that now there are two requests sent, first an html then the same js request.
Started GET "/users/4/message"
Processing by StudentsController#message as HTML
...
Completed 406 Not Acceptable in 3ms (ActiveRecord: 1.0ms)
Started GET "/users/4/message"
Processing by StudentsController#message as JS
The html request throws the missing template error. Does anyone know why refreshing the page causes rails to attempt to respond with an html request to a remote link?
EDIT: routes.rb
resources :students do
member do
get 'message'
end
end
Does your app/assets/javascripts/application.js contain
//= require jquery
//= require jquery_ujs
?
And your erb contains <%= javascript_include_tag "application" %>?
I was just struggling with a problem like this for HOURS and the last of those two points fixed it; I saw the first point mentioned in some other questions so I'll repeat it here.
Hope that helps.
(Credit where credit's due)
What solver it for me was adding :format => "js"
So in your case:
<%=link_to "Message", message_user_path(:id => user.id, :format => "js"), :remote => true %>
In general, when you use link_to on a particular button or so, when you press the button, as js request is send to the controller, but also searches for the respective .js.erb file and so on.
My solution was to replace
format.json do
with
format.js do
you can troubleshoot the request by setting a breakpoint (i use pry) in the controller and then look at the variable
request.format
For newer versions of Rails, this should be fixed where using remote: true within the link_to code, as the original poster was doing, will only look for a .js format to respond with. As others have said, if you never need an html response, then you can remove that from your code all together; you won't even need a respond_to, respond_with, etc as Rails will auto respond with JS looking for the template you already have made. So your controller code would look like this:
def message
#user_id = params[:id]
#user = User.find_by_id(#user_id)
end
And the link would still be this:
<%=link_to "Message", message_user_path(:id => user.id), :remote => true %>
Or this would work as well (my preferred way of syntax):
<%=link_to "Message", message_user_path(id: user.id), remote: true %>
This code will call the controller action which will look for the template message.js.erb.
I know this question is old now, but for anyone looking for answers and using current Rails 6+ (I'm using 7.0.0alpha), and if you are getting this same type of issue where both HTML and JS templates are being requested; check that turbolinks is not what is causing the issue. Sometimes turbolinks can cause a request to be sent twice and it may be sending the first request as an HTML request.
My form with remote: true was inside another form and I didn't know it.
So make sure it isn't inside another form.

How do I correctly handle redirects for bad url's to custom 404 page in ruby on rails?

Users can access others users profiles like this:
site.com/username
My find method in my users controller will find the user by their username.. now if the user doesn't exist I want them re-directed to a custom 404 error page I'll make.
I also want them re-directed for any other non-existant url's they type in.
I've found some solutions on google but wondering if someone can give me an up to date example of doing this in rails 3.2 as theses tutorials I've found are pre rails 3.1.
Kind regards
I tried this solution with Rails 3.2 and it just works.
https://makandracards.com/makandra/12807-custom-error-pages-in-rails-3-2
In your config/application.rb
config.exceptions_app = self.routes
In your config/routes.rb
get '/404', to: 'errors#not_found'
get '/500', to: 'errors#server_error'
Create app/controllers/errors_controller.rb
class ErrorsController < ApplicationController
def not_found
render status: 404
end
def server_error
render status: 500
end
end
Create app/views/errors/not_found.html.haml and app/views/errors/server_error.html.haml (replace haml by erb if you do not use haml)
Restart your server
In production an ActiveRecord::RecordNotFound exception will automatically render your public/404.html file.
you can always render template 404 along with status code when record is not found or invalid URL entered
render :template => 'public/404.html', :status => 404
Here is the blog for "custom dynamic error pages in ruby on rails" will help you
custom dynamic error pages in ruby on rails
isnt it just in /public/404.html ?
UPDATE
Actually im pretty sure it is, that the same exact 404 page that comes up while running my page on nginx in production mode.

link_to_function in rails 3.1 with blocks throws wrong number of arguments error

I recently started working with rails 3. I'm trying to add a multi-modal form into my application. I'm following the steps mentioned at Handle multiple models in one form
When I try to add a link via following helper function, I get
wrong number of arguments (1 for 2)
Code block below.
def add_task_link(name)
link_to_function name do |page|
page.insert_html :bottom, :tasks, :partial => 'task' , :object => Task.new
end
en
Googling for solution didn't take me anywhere.
Thank you.
You should find another tutorial, RJS and prototype are deprecated in rails 3, they've been replaced with jQuery.
If you really insist on going forward, you can bring RJS and prototype back by adding the following to your Gemfile:
gem 'prototype-rails'
This will bring back the correct version of link_to_function.

Resources