user_signed_in? is causing uninitialized constant error with Mongoid / Devise - ruby-on-rails

I had some issues with a different ODM yesterday so I decided to take a look at Mongoid instead. The wiki tutorial was pretty thorough, so it seemed like it was worth a shot. I went through the wiki article very carefully but may have missed something. Perhaps this is happening because I had a different ODM in the app yesterday. Anyway, now I'm getting an error:
uninitialized constant User::Mongoid
with this line of code (threw this in to confirm it was this method that was the issue):
<% if user_signed_in? -%>
5: Hello user
6: <% end -%>
user_signed_in is a Devise method. I suspect that Devise isn't actually loading but I'm not sure what to do next.
My gem file has gem "devise", "1.5.0" and I've run a bundler install, I can see that devise is getting installed. I've confirmed that my application controller exactly matches the app controller in the sample. My app starts with no issues when I run rails server. All the models in the app load correctly when I do load 'app/models/user.rb'. The file config/initializers/devise.rb has require 'devise/orm/mongoid'. I've verified that my User model exactly matches the one in the tutorial.
What should I look at next?
Update: This was fixed, but I'm still missing how. I started the app on a new port. That's all it took. I was making changes to my index.html.erb and seeing those changes in the error that I was getting so I know the new code was being seen. But when I started the app on a new port the error went away. Perhaps there's some sort of 'hard recompile' in rails that I'm not aware of?

First, confirm that the error is still happening after you fully restart your rails instance.
I suppose that in the User model you have something like this:
class User
include Mongoid::Document
devise :database_authenticatable, ...
end
Check in config/initializers/devise.rb:
# ==> ORM configuration
# Load and configure the ORM. Supports :active_record (default) and
# :mongoid (bson_ext recommended) by default. Other ORMs may be
# available as additional gems.
require 'devise/orm/mongoid'
Also check that devise gem is under mongoid gem in Gemfile:
gem "mongoid", "~> 2.3.2"
gem "bson_ext"
gem "devise", "~> 1.4.9"

Related

How can I alter a gem on heroku?

I recently deployed my app to heroku. It uses Devise, which I had to alter the after_sign_up_path so it would redirect correctly to a set page after someone signs up. This works fine locally, but when I deploy on heroku it installs the original unmodified devise gem.
Does anyone know of a way to modify a gem on heroku, or to make it use the modified gem in my vendor folder?
Thanks.
So, in general, it shouldn't be any different deploying your app on heroku than anywhere else.
One difference is that on heroku you're probably running in 'production' environment settings, and locally in 'development'. You can run in production locally too, if that were the issue.
But I don't think it is. When you say you "altered" Devise, do you mean you actually edited the source code inside the Devise gem source itself?
That's not a good practice. When a new version of Devise comes out, perhaps with security patches, what are you going to do? Re-customize the new version? It's not a great maintenance plan. If you really want to do this you can. The best way might be to create a fork of the Devise gem in a git repo (on github or anywhere else), customize that and commit your customizations to the repo, and then point to your customized version of Devise from your own git repo in your Gemfile with gem 'devise', :git => 'http://some.git.repo.clone.url.org'
But it's a bad idea.
Devise probably already supports your use case with configuration, rather than by editing Devise source code. Most experienced devs would consider that a much preferable way to accomplish what you want.
In this case, it looks like you should be providing an after_signup_path method override in your own local app, not modifying the default implementation in devise source. Google for more info, or consult the URL Finks provides in another answer, or post another question specifically asking how to do what you want to do with devise (it's actually nothing to do with heroku), being as specific as possible about what you want to accomplish.
Assuming you forked Devise to your github account, you can point your Gemfile to it by doing this:
gem "devise", :git => "git://github.com/user/devise.git", :branch => "my-awesome-branch"
By the way, I'd recommend you overwrite some devise methods rather than hack the gem but thats just my 2cents.
This may not answer your question but according to devise wiki, you don't have to alter the gem to get the redirect behavior you like, you just need to override the after_sign_in_path_for method in application controller. https://github.com/plataformatec/devise/wiki/How-To%3A-Redirect-to-a-specific-page-on-successful-sign-in-and-sign-out
Example:
def after_sign_in_path_for(resource)
current_user_path
end

how to set up nulldb in rails with rspec?

I'm almost new to rails development. I'm currently reading Avdi Grimm's Objects on Rails for having a #SOLID design in my rails apps instead of being forced to some conventions which will create mess and unreadable code and design.
I wanna setup nulldb and use it in my fast specs which are testing the business logic of the application but I can't get it to work. I read the installation guide at nulldb GitHub page here -> https://github.com/nulldb/nulldb. I installed the activerecord-nulldb-adapter gem and put it also in my Gemfile and ran the bundle install command so it's completely installed now. I have a spec_helper_lite.rb file which I use in my fast specs so I though it's a good idea to setup nulldb in it. Here's the code for nulldb part in my spec_helper_lite.rb file:
require 'nulldb_rspec'
def setup_nulldb
schema_path = File.expand_path("../db/schema.rb", File.dirname(__FILE__))
ActiveRecord::Base.establish_connection(:adapter => :nulldb,
:schema => schema_path)
NullDB.nullify(:schema => schema_path)
end
def teardown_nulldb
NullDB.restore
end
then I require this spec_helper_lite.rb in my fast specs and I call the setup and teardown nulldb method in before and after methods of my spec. when I run the specs I'll get the error Uninitialized Constant ActiveRecord::ConnectionNotEstablished (NameError). I tried different things like removing that establish_connection line in the setup_nulldb and I'll get the same error. I even required 'active_record' in my spec_helper_lite just to see what will happen and then I'll get the error "undefined method nullify" for NullDB module and obviously the spec became completely slow cause of requiring active_record. I searched a lot about how to setup nulldb and everything I saw explained about setting it up this way but it doesn't work for me. I use nulldb version 0.2.1, rails 3.0.0 and rspec 2.0.1
I appreciate your help about how to set this up correctly. Thanks in advance.
Sam
I was having similar issues (but using minitest, and activerecord independent of rails). I tried all the same stuff you mentioned. Turns out I was just using whatever was in rubygems. It looks like updating my Gemfile to pull directly from github resolved the issue:
group :development, :test do
gem 'activerecord-nulldb-adapter', :git => 'git://github.com/nulldb/nulldb.git'
end
You'll want to bundle install again after updating.

devise and invitable plugin : how do you set the invitation_limit

Hi ive got devise and the invitable plugin working but Id like to set a default limit on how many people a user can invite.
According to the documentaion on github it mentions that you can set this number via invitation_limit
however ive tried using this in devises config file but it complains of undefined method.
I checked the source and theres definately an invitation_limit attribute being decremented. I tried adding this as an attribute to my users model but it still complains.
How do you setup this up????
My guess is that you are using the version of the devise_invitable gem that does not include "invitation_limit." You will need to use at least v0.4.rc5 to get all the new coolness that the documentation talks about. It's definitely a little confusing. Your Gemfile should look like this:
gem 'devise', '~>1.2.0'
gem 'devise_invitable', '~>0.4.rc5'
See here for a more in depth write-up about this issue with devise_invitable.

Installing and using acts-as-taggable-on

This is going to be a really dumb question, I just know it, but I'm going to ask anyways because it's driving me crazy.
How do I get acts-as-taggable-on to work?
I installed it as a gem with gem install acts-as-taggable-on because I can't ever seem to get installing plugins to work, but that's a whole other batch of questions that are all probably really dumb. Anyways, no problems there, it installed correctly.
I did ruby script/generate acts_as_taggable_on_migration and rake db:migrate, again no problems.
I added acts_as_taggable to the model I want to use tags with, started up the server and then loaded the index for the model just to see if what I've got so far is working and got the following error: undefined local variable or method `acts_as_taggable' for #.
I figure that just means I need to do something like require 'acts-as-taggable-on' to my model's file because that's typically what's necessary for gems. So I did that hit refresh and got uninitialized constant ActiveRecord::VERSION. I'm not even going to pretend to begin to know what that means went wrong.
Did I go wrong somewhere or there something else I need to do. The installation instructions seem to me like they just assume you generally know what you're doing and don't even begin to explain what to do when things go wrong.
Did you try to define your gem dependencies in config/environment.rb (Rails 2.3):
Rails::Initializer.run do |config|
#...
config.gem 'acts-as-taggable-on'
#...
end
Or in Gemfile for Rails 3 or if you use already Bundler with rails 2.3:
gem 'acts-as-taggable-on'
This should make the require 'acts-as-taggable-on' unnecessary
Maybe following the installation here can help.
For example you don't need to:
require 'acts-as-taggable-on'
but:
class User < ActiveRecord::Base
acts_as_taggable
end
Otherwise you need to post more details about the error.
I installed acts-as-taggable-on for my app through github. If you want to try that method instead of the gem, you can read my this post that explains my experience: http://blog.mediummassage.com/2010/04/27/creating-categories-in-the-store-with-tags/

Any working tutorials for Authlogic?

I've been trying to build my first rails app and have gotten stuck on the issue of user authentication. I've found a number of tutorials for using various plug-ins to do this, but so far every single one of them is out-dated, and as a result, broken!
From what I've read, I think Authlogic may be the best fit for me, and I've tried two things:
1) Going through Railscast, episode #160 (which is a tutorial for setting it up)
2) Using Ryan B's nifty_authentication gem with the --authlogic tag
In both cases, I get the following error as soon as I try to do anything with a user:
undefined local variable or method `acts_as_authentic' for #
I believe this is from the User model:
class User < ActiveRecord::Base
acts_as_authentic
end
I'm sure I've installed the authlogic gem, and I've added
config.gem "authlogic"
to my environment.rb
Any ideas about what's wrong? Anybody know of a complete and up to date tutorial for adding user authentication?
Edit:
I'm running Ruby v. 1.8.6 and rails v. 2.3.5
There is one thing that Ryan Bates in the RailsCasts episode doesn't talk about is about creating sessions table in your database. Type rake db:sessions:create in the console and run the migration rake db:migrate. Also like ghoppe says run rake gems:install after installing the gem. That is a requisite.
Here's an example app with a step-by-step guide - it's from last year but should still be mostly if not entirely accurate:
authlogic_example
Since you added that line to your environment.rb, have you tried rake gems:install to ensure the gem is installed and working correctly?
Also, what version of Ruby? What version of Rails? Have you tried running gem environment and gem list to make sure they're installed and running from the right place?
Another option is to use authlogic as a plugin, with:
script/plugin install git://github.com/binarylogic/authlogic.git
It also helps to look at a projects that uses authlogic as authentication module, like the fat_free_crm project, have a look at user.rb there
Last but not least, there is an active mailing list:
authlogic mailing list
Becoming popular is also the devise gem. Here you can add authentication with script/generate devise and you will have some views for login as well.
I forked that authlogic_example and added activity_tracker, authlogic, paperclip for user profile images, declarative_authorization, and user to user messages.
http://github.com/jspooner/authlogic_cucumber_rspec_example

Resources