Active_admin NoMethodError - ruby-on-rails

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.

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

will_paginate causes ActiveRecord::RecordNotFound in show

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.

Getting Formtastic in ActiveAdmin to use custom form input builder

UPDATE: So, I've found this, and apparently that's why this old way of doing things isn't working, ActiveAdmin must use Formtastic 2.x. As directed, I've now created a file in app/inputs/date_picker_input.rb that looks like this:
class DatePickerInput
include Formtastic::Inputs::Base
def to_html
puts "this is my datepickerinput"
end
end
And my controller now looks like this:
f.input :open_date, :as => :date_picker
f.input :close_date, :as => :date_picker
But now I'm running into this error:
ActionView::Template::Error (undefined method 'html_safe' for nil:NilClass):
1: render renderer_for(:edit)
Any thoughts?
I've ran into a problem with Formtastic automatically formatting dates into a format I'm not wanting (Y-m-d h:i:s Z) when I try to render :as => string so I can use a datepicker on the field. In trying to solve this, I came across this solution.
It seems to make sense and is the exact same problem I am dealing with. However, I don't seem to be able to implement the fix, and I'm wondering if it's because Formtastic is being used through ActiveAdmin. So, here's what I've tried to do:
In the controller, I've changed the method as such:
f.input :open_date, :as => :date
I also tried this, though my problem is not even being able to get to this point:
f.input :open_date, :as => :date_input
I created a file at lib/datebuilder.rb with the following code:
class DateBuilder < Formtastic::SemanticFormBuilder
def date_input(method, options)
current_value = #object.send(method)
html_options ||= {:value => current_value ? I18n.l (current_value) : #object.send("#{method}_before_type_cast")}
self.label(method, options.delete(:label), options.slice (:required)) +
self.send(:text_field, method, html_options)
end
end
I'm not sure that will even fix the format as I want, but I assume if I can just get Formtastic to use this method I can alter it as needed (currently took this from the solution mentioned in link above).
This article mentions you need to add a line to your formtastic intializer to use this custom input:
Formtastic::SemanticFormHelper.builder = Formtastic::DateBuilder
I did not have this initializer file in config/initializers so I added it (config/initializers/formtastic.rb) with the line above. The problem I am now running into is this error when trying to startup the Rails app:
../config/initializers/formtastic.rb:1:in '<top (required)>': uninitialized constant Formtastic::SemanticFormHelper (NameError)
I have also tried this syntax in that file instead:
module Formtastic
module SemanticFormHelper
self.builder = DateBuilder
end
end
which gives me this error instead: ../config/initializers/formtastic.rb:3:in '<module:SemanticFormHelper>': uninitialized constant Formtastic::SemanticFormHelper::DateBuilder (NameError)
If I'm going about this in completely the wrong way please let me know, otherwise any help on getting Formtastic to use this custom input type would be amazing!
Alright, finally figured out the right way to do this.
My controller stayed the same as seen above in the update. However, here's what I changed the DatePicker custom input file (app/inputs/date_picker_input.rb) to look like:
class DatePickerInput < Formtastic::Inputs::StringInput
def to_html
"<li class='string input required stringish' id='question_#{method.to_s}_input'>" +
"<label class=' label' for='question_#{method.to_s}'>#{method.to_s.gsub!(/_/, ' ').titleize}*</label>" +
"<input id='question_#{method.to_s}' name='question[#{method.to_s}]' type='text' value='#{object.send(method)}' class='hasDatePicker'>" +
"</li>"
end
end
Hopefully this will help someone else running into the same problem! Btw, the hard-coded "question" and "required" is because I will only use this custom input on question objects. There is likely a way to dynamically get this information, but I decided against putting more work in to figure that out (this was enough of a headache on its own!)

will_paginate and having problems getting to work

I'm getting errors on will_paginate such as: undefined method `paginate' for # in my rails app.
I'm trying to do the simplest possible query such as:
#results = Location.paginate(:page => params[:page])
or
#results = Location.paginate(:page => params[:page]).order('name')
Location.order('name') works fine in console
Any ideas what is going on or next steps? Does anything need to be added to the model?
Have you configured everything as outlined in the docs? I remember when I first got started with rails and will_paginate, I had to restart the web server for everything to work.

Dynamic Find By In Rails 3

So, I have a very simple bit of code that works great in Rails 2, but breaks in Rails 3.
In Rails 2 I have the following for displaying page contents using a viewer controller from the Page model:
class ViewerController < ApplicationController
show
#page = Page.find_by_name(params[:name])
end
My viewer show view has the following:
<%= #page.body %>
My routes.rb file has the following to handle this action:
map.view_page ":name", :controller => 'viewer', :action => 'show'
Here is the error I get in Rails 3 using this code:
undefined method `body' for nil:NilClass
Now, I know the routes have to be changed in Rails 3, but what else am I missing to make this simple code work in a Rails 3 app? I can't seem to find the answer anywhere. Thanks!
match ":name", :to => "viewer#show", :as => "view_page"
Not tested not sure if it's match "/:name" or if ":name" is ok too.
Ok, Rails 3 have got new routing sintax
get "/:name" => 'viewer#show'
Useful links:
http://railscasts.com/episodes/203-routing-in-rails-3
http://guides.rubyonrails.org/routing.html
http://www.engineyard.com/blog/2010/the-lowdown-on-routes-in-rails-3/
UPD
Your error is because Rails can't find any Page with your :name:
There is no any Page with this name
There is no any :name at all in your params hash
Make sure your database has the body field filled with something and it should work just like you have it.
domain.com/viewer/show?name=home

Resources