Rails Active Admin resource problem - ruby-on-rails-3

I recently have watched railscast 284 about active admin and wanted to implement it into my web app, however I am running into an issue when I add a resource. I get the following message every time I try to navigate to the created tab:
NameError in Admin::LoadsController#index
undefined local variable or method `per' for []:ActiveRecord::Relation
Rails.root: /Users/thomascioppettini/rails_projects/want-freight
Application Trace | Framework Trace | Full Trace
Request
Parameters:
{"order"=>"id_desc"}
Show session dump
Show env dump
Response
Headers:
None
The only thing I can think of that may affect the application is adding a recaptcha to devise, which active admin depends on.

For me, it looks like this is a pagination problem. What gem are you using? You should give as more details about your settup. Can you show us your resource file from admin directory? What version of rails and what ActiveAdmin are you using ?

If you are using the will_paginate gem, set the version to 3.0.pre2. I was using ~>3.0.pre2, which auto-updated to 3.0.2 when I ran a bundle update Reverting fixed the issue. If you're using Bundler, the line is this:
gem "will_paginate", "3.0.pre2"

I agree with Dawaid. It is a pagiantion error. Add "Kaminari" gem to you Gemfile. According to active admin docs, it is using kaminari for pagination.. will_paginate will also work for you as swilliams described...

As I understand active_admin doesn't support will_paginate anymore. But if you don't want to rewrite your pagination to Kaminari you can fix this problem with putting some code to initializers
# config/initializers/will_paginate.rb
if defined?(WillPaginate)
module WillPaginate
module ActiveRecord
module RelationMethods
alias_method :per, :per_page
alias_method :num_pages, :total_pages
end
end
end
end
module ActiveRecord
class Relation
alias_method :total_count, :count
end
end

Related

How to set sinatra-authentication to use erb instead of haml?

I'm trying to set up the sinatra-authentication gem in a simple sinatra app, and running into an issue where sinatra can't find the correct views. I understand that sinatra-authentication uses haml by default, but I'm using erb in this app.
This in mind, I found in the sinatra-authenticaiton docs that there is a setting which allows you to change the template engine, by adding the following to your app file:
configure do
set :template_engine, :erb # for example
end
I've added this to my app.rb file, and sinatra is still looking for the signup.haml when I try to hit the /signup route in my app.
A couple of notes:
I've included the gem in my Gemfile, and successfuly run a bundle install on my app.
source 'https://rubygems.org'
gem 'sinatra'
gem 'data_mapper'
gem 'pg'
gem 'dm-postgres-adapter'
gem 'sinatra-authentication'
I saw something in the documentation that suggested that I may need to specify the location of my view files, so I added the following to my configuration block.
set :sinatra_authentication_view_path, Pathname(__FILE__).dirname.expand_path + "views/"
**I think I've required the gem accurately in my app file by adding
require "sinatra-authentication"
use Rack::Session::Cookie, :secret => 'mys3cr3tk3y'
This gist is a current representation of my app.rb file in the root of my sinatra app. https://gist.github.com/rriggin/5378641#file-gistfile1-txt
Here is a screenshot of the error sinatra throws: http://cl.ly/image/0y041t0K3u3O
When I run the app locally, a 'dm-users' table is created in my local db as expected.
Is there another configuration setting that I'm missing in order to get sinatra-authentication to properly look for the erb templates rather than haml files. Any help would be greatly appreciated.
Thanks
The specs don't test that the template_engine setting works, and looking at the way the setting is called, I believe it's not correct, i.e.
send settings.template_engine, get_view_as_string("index.#{settings.template_engine}"), :layout => use_layout?
might better work as:
send app.settings.template_engine, get_view_as_string("index.#{app.settings.template_engine}"), :layout => use_layout?
that's what I reckon. If you fork the project, change the line and add it to your Gemfile and it works then consider writing a quick spec for it and you'll have improved the mainline of that project as well as fixed your problem.

Gem with models

I created a Gem with models (actually, extracted it from the main project) to share amongst the projects we have in our platform.
We have dozens of models, so instead of requiring them one by one, I wrote the following code:
Gem.find_files("my_gem/models/*.rb").each { |path| require path }
I access one of the projects that has my_gem in the gem file and running rails c I get the following output:
/Users/myuser/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/dynamic_matchers.rb:50:in `method_missing': undefined method `has_attached_file' for #<Class:0x007fad4b93ccb8> (NoMethodError)
One of my models is using the gem paperclip, what is weird is:
If I remove the line declared above to load all the models
automatically, rails c runs fine
If I try to include onlye the model that uses paperclip require
"my_gem/models/paperclip_model" I receive the same error
So then I change my gem to not load any model, and when I try to reference any model from rails console, it says the class is not loaded, but then I run Gem.find_files("my_gem/models/*.rb").each { |path| require path } or require "my_gem/models/paperclip_model" 'they work perfectly and I am able to work with the model.
Has any of you seen the same issue?
Seems that changing require for autoload solved the problem
I changed Gem.find_files("my_gem/models/*.rb").each { |path| require path }
for
Gem.find_files("my_gem/models/*.rb").each do |f|
filename = File.basename(f, '.*')
class_name_symbol = filename.classify.to_sym
autoload class_name_symbol, "my_gem/models/#{filename}"
end
and now it is working.
It sounds like one of the models in your gem depends on Paperclip, but you don't explicitly set it as a dependency. So what's happening is that if your models get loaded before paperclip gets loaded, you'll see the UndefinedMethod error for has_attached_file.
If you use your models in a Rails application which has paperclip as a dependency, and you load those models after the console (or server) has spun up, Paperclip will be present, so you won't see this error.
The solution is to explicitly add paperclip as a dependency in your gemspec, something like:
s.add_dependency('paperclip')
Assuming that this gem will always be used in the context of a Rails application, this should work. If not, you might also need to add the following line to the top of your models that use paperclip:
require "paperclip"

Sunspot_Rails - undefined method `searchable' on page

I have a model with a searchable block, like so:
class Contact < ActiveRecord::Base
searchable do
text :contact_name, :company_name, :contact_email
end
end
In the controller's index action, I'm calling Contact.new, which is giving me an error message on the page, which is currently running on our Staging server:
undefined method `searchable' for #<Class:0xce0bf80>
The stack trace is pointing to the searchable block in the Model via the Contact.new line in the controller.
When I run the code locally, either on the webpage or the console, or through the console on the Staging server, this error isn't appearing - only on the Staging webpage.
The Solr service is running fine on the Staging server, and the data has been indexed successfully. Any theories as to why it's not playing ball in Staging will be accepted.
EDIT
In response to Nick's question below, the Gemfile just has this line for Sunspot: gem 'sunspot_rails'
For Gemfile.lock, these are all the lines I could spot containing Sunspot or Solr references:
GEM
rsolr (0.12.1)
builder (>= 2.1.2)
sunspot (1.2.1)
escape (= 0.0.4)
pr_geohash (~> 1.0)
rsolr (= 0.12.1)
sunspot_rails (1.2.1)
nokogiri
sunspot (= 1.2.1)
DEPENDENCIES
sunspot_rails
I had the same problem, and I just had to restart my rails server. Simple solution, but if it wasn't for another post somewhere that suggested I do so, I would probably have tried to debug the error for a much longer time before just trying to restart the server, hehe...
This can also happen if you forget to restart your rails server after installing the new sunspot gem
Sounds to me like the gem isn't being loaded correctly on your staging site. If you can show the relevant sections of your Gemfile and Gemfile.lock, I can update with more of an answer.
EDIT — Gemfile looks fine. Sorry, but I've got nothin' without being able to get my hands on the app. Report a bug to the Sunspot mailing list? http://outoftime.github.com/sunspot
After reading this post
I understood that the problem is because the rake tasks line prevents the loading of Sunspot
I managed to solve it in the following way:
I removed the following line from my Rakefile
require 'sunspot/rails/tasks'
I created a sunspot.rake file and added to it the contents of the following file from the sunspot gem source:
/gems/sunspot_rails-1.2.1/lib/sunspot/rails/tasks.rb
I know that this a hack, but it is working.
I just ran spring stop and it fixed it

OmniAuth Invalid Response - Updating to 1.9.2 not working

Hi
I know this question have been asked before but the answers there isn't working for me.
I still get the, when redirecting back to my site.
/auth/failure?message=invalid_response
I have ruby 1.9.2p180 (2011-02-18 revision 30909) [x86_64-darwin10.6.0] installed, using rails 3.0.7 and the required gems installed. I read on another thread that you should have pure_json added to the gemfile to make it work. But that didn't help me either.
I'm clueless... Thanks in advance
authenticationscontroller
def index
#authentications = current_user.authentications if current_user
end
def create
#render :text => request.env["omniauth.auth"].to_yaml
auth = request.env["omniauth.auth"]
current_user.authentications.find_or_create_by_provider_and_uid(auth['provider'], auth['uid'])
flash[:notice] = "Authentication successful."
redirect_to authentications_url
end
I was able to get this to work by specifying the following in my Gemfile
gem 'omniauth', '0.2.0'
Version 0.2.6 wouldn't work for me. I also updated by .rvmrc to rvm use 1.9.2#rails3. Oh, also make sure you're logged in - do note that in your code above you are assuming that current_user exists. See Ryan's Railscast part two for allowing user creation via Omniauth.
I've got a demo working here, but do note I'm doing authentication from scratch rather than using Devise.

Rails app error after updating OS

I updated my system to opensuse 11.4 and set up Ruby 1.9.2 via RVM. I reinstalled all the gems and updated bundle all without issue.
The problem is the vague error I get when running the app:
ActionController::RoutingError (undefined method `sub' for nil:NilClass):
app/controllers/application_controller.rb:1:in `<top (required)>'
app/controllers/news_controller.rb:1:in `<top (required)>'
I don't even know where to start looking for the problem. Both files on line 1 is the class declaration. ie class NewsController < ApplicationController and class ApplicationController < ActionController::Base. Neither files have a method call to 'sub' and no other information is given.
The app worked perfectly before the upgrade (which was using Rails 3.0.5 also) so I think the issue is somewhere in Rails, except running a new application with a simple scaffold has no problems. news#index is root in the routes file, but changing root to something else does nothing.
EDIT:
resources :categories,:addresses,:calendars,:topics,:profile,:news,:account_setting
resources :boards do
member do
get :move
post :move_category
end
end
get "user/index"
get 'login/index'
get 'login/new'
post 'login/create'
post 'login/authenticate'
get 'login/forgot_password'
put 'login/reset_password'
root :to => "news#index"
No need to do all that. I think the solution was probably a lot simpler. I just got the same error. Turns out I just had a misnamed helper module:
module AssetHelper
...
end
Should've been
module AssetsHelper
...
end
I got it resolved, although I don't know what caused it.
I created a new app and copied over app, routes, db, lib, and public and the problem disappeared.