Problem with forgot password and authlogic - ruby-on-rails

I'm having a few problems with the forgot password system from this tutorial.
My application uses Authlogic for the authentication system and it works perfectly for user login/logout and registrations. However after I followed that tutorial to the letter (password_reset controller renamed to 'reset' and I used my own existing mailer configuration), and tried to reset my test accounts password, I get a "wrong number of arguments 1 for 0" error on my reset controller's create action.
ArgumentError in ResetsController#create
wrong number of arguments (1 for 0)
c:/Ruby/lib/ruby/gems/1.8/gems/actionmailer-2.2.2/lib/action_mailer/base.rb:462:in `password_reset_instructions'
c:/Ruby/lib/ruby/gems/1.8/gems/actionmailer-2.2.2/lib/action_mailer/base.rb:462:in `__send__'
c:/Ruby/lib/ruby/gems/1.8/gems/actionmailer-2.2.2/lib/action_mailer/base.rb:462:in `create!'
c:/Ruby/lib/ruby/gems/1.8/gems/actionmailer-2.2.2/lib/action_mailer/base.rb:455:in `initialize'
c:/Ruby/lib/ruby/gems/1.8/gems/actionmailer-2.2.2/lib/action_mailer/base.rb:392:in `new'
c:/Ruby/lib/ruby/gems/1.8/gems/actionmailer-2.2.2/lib/action_mailer/base.rb:392:in `method_missing'
app/models/user.rb:7:in `deliver_password_reset_instructions!'
app/controllers/resets_controller.rb:12:in `create'
I have stared at my code for a couple of hours, trying various tweaks, Google'd the issue, browsed this site, but I still don't know why this is happening and would appreciate any pointers your guys can provide.
Thanks in advance for your help!
I am using ruby 1.8.6, Rails 2.2.2 and Authlogic 2.1.5

If I'm reading that trace properly then it looks to me like password_reset_instructions has been declared as a no-argument method. That's what the (1 for 0) complaint is about. Can you check that you have included the user argument in the definition, as below?
class Notifier < ActionMailer::Base
def password_reset_instructions(user)
end
end
If you've been staring at it for hours it's probably not that simple but worth making sure.

Related

Capybara + Sorcery helpers

I'm currently working on a project using Sorcery to authenticate users and I have some integration tests done with capybara. When I call current_user or logged_in? sorcery helpers I get this error:
ArgumentError Exception: wrong number of arguments calling `page` (0 for 1)
but when I call them in development mode, it doesn't happen.
After some research I found out that the problem comes from find_by_id method. So if I call Model.find_by_id(1), I get this error. I am very confused because I cannot understand why page method is called and where.
I fixed it by overriding the find_by_id method of my model, but I completely disagree doing this way, so...
Does anybody know what's going on?
Thank you in advance guys
I finally fixed it. The problem was that I included Capybara in my env.rb so my ActiveRecord class were inheriting Capybara methods and find_by_id method for finding elements in a webpage was run instead of ActiveRecord find_by_id.
After deleting "include Capybara" line everything works fine.

Devise throws undefined user for nil class in tests

I am using the Devise Gem in version 3.2.2 for authentication in my Rails 3.2.17 app. Now I am building an API and securing it with devise using the method suggested in this Gist.
Now when I am doing a get request on a secured controller using curl (in this case with a UI) I get a 200 and the response I was looking for. For some reasons my tests fail and produce an error like this:
1) Api::V1::UsersController#information should get the dancing partner
Failure/Error: get :information, {}
NoMethodError:
undefined method `user' for nil:NilClass
# ./app/controllers/api/v1/api_controller.rb:26:in `authenticate_user_from_token!'
# ./spec/controllers/api/users_controller_spec.rb:24:in `block (3 levels) in <top (required)>'
I don't know if this is an issue caused by devise or by something else, so I attached all changed code at the bottom of this post.
Here is an example how it should work:
This is my Api controller, which should secure the normal controllers used for the api.
This is my secured controller
Here are my tests
This is my authentication helper
This is my test helper
The complete output of log/test.log
I just figured out, that this error came from accessing the user variable at a point, where I was testing the response for an invalid authentication.

Rails 3.1.0.rc4 + Postgres - cannot read from or write to database after reboot

I've just started to write an application using the latest version of Rails and PostgreSQL. I created the database, added required gem, configured database.yml file and started with two models - User (this one used Devise for authentication) and Group. I created an additional controller for start page (simple one - only to display list of links). Everything seemed fine, I was able to add test data to the database - until I came back this morning and wanted to continue work.
As long as I stayed on the home page, everything looked just like yesterday. But when I tried to access group list, I got the following error:
Routing Error
You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.map
There was no additional informations on the page, so I looked into Webrick console and saw the following:
ActionController::RoutingError (You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.map):
app/controllers/groups_controller.rb:1:in `<top (required)>'
The first line of my controller is, as usual:
class GroupsController < ApplicationController
I looked at the other actions, and the result was the same: unexpected nil object. Same issue occured while trying to perform any action on User.
I suspected it's a database problem (because it didn't affect the controller that wasn't using database at all), so I went to rails console to see if I could add entries manually. I couldn't.
ruby-1.9.2-p180 > group = Group.new
(some SQL)
NoMethodError: You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.map
from /home/lite/.rvm/gems/ruby-1.9.2-p180/gems/activerecord-3.1.0.rc4/lib/active_record/persistence.rb:320:in `attributes_from_column_definition'
from /home/lite/.rvm/gems/ruby-1.9.2-p180/gems/activerecord-3.1.0.rc4/lib/active_record/locking/optimistic.rb:69:in `attributes_from_column_definition'
from /home/lite/.rvm/gems/ruby-1.9.2-p180/gems/activerecord-3.1.0.rc4/lib/active_record/base.rb:1525:in `initialize'
from (irb):1:in `new'
from (irb):1
from /home/lite/.rvm/gems/ruby-1.9.2-p180/gems/railties-3.1.0.rc4/lib/rails/commands/console.rb:45:in `start'
from /home/lite/.rvm/gems/ruby-1.9.2-p180/gems/railties-3.1.0.rc4/lib/rails/commands/console.rb:8:in `start'
from /home/lite/.rvm/gems/ruby-1.9.2-p180/gems/railties-3.1.0.rc4/lib/rails/commands.rb:40:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
I looked into the most top file (persistence.rb) and searched for line 320.
319: def attributes_from_column_definition
320: Hash[self.class.columns.map do |column|
321: [column.name, column.default]
322: end]
323: end
This definition gave me a little idea what might be happening, so I ran one more command in the console (Group.inspect) and I got error on the following line:
attr_list = columns.map { |c| "#{c.name}: #{c.type}" } * ', '
It seems like I'm not able to access the columns of my table, but I have no idea why. I'm logged in as the same user, on the same machine, using the same operating system and kernel. Out of curiosity, I created another application and it didn't work after the reboot either.
I've spent now four hours looking for answer, but I couldn't find anything related. What might be causing this problem and how to fix it?
I have found the guilty one: small gem called 'automatic_foreign_key' I used to automatically detect foreign keys. When I rollback the changes it made, removed it and altered the table manually, I had my application working again.
Thank you all for help!
Maybe Group is a reserved word in this version of postgresql or rails. rails-3.1.0-rc4 is the most fresh version of rails. Generate another dead simply model to check it.

What does "Anonymous modules have no name to be referenced by" really mean?

I'm upgrading my Rails app to work with Ruby 1.9 and I keep encountering errors like this:
Anonymous modules have no name to be referenced by
/home/foo/.gem/ruby/1.9.1/gems/activesupport-2.3.8/lib/active_support/dependencies.rb:585:in `to_constant_name'
/home/foo/.gem/ruby/1.9.1/gems/activesupport-2.3.8/lib/active_support/dependencies.rb:391:in `qualified_name_for'
/home/foo/.gem/ruby/1.9.1/gems/activesupport-2.3.8/lib/active_support/dependencies.rb:104:in `rescue in const_missing'
/home/foo/.gem/ruby/1.9.1/gems/activesupport-2.3.8/lib/active_support/dependencies.rb:94:in `const_missing'
/home/foo/app/config/environment.rb:66:in `block in <top (required)>'
etc.
Google finds all kinds of hits for this, but each of them pertains to a specific fix for one specific gem or app. None of them explain what the message really means.
What is an "anonymous module"?
Where is this error message coming from? (The Ruby interpreter itself?)
What is different about Ruby 1.9 that causes this? (Rails 2.3.8 with Ruby 1.8.7 does not encounter this.)
What is the general/proper way to fix this error?
Line 66 of environment.rb is the configuration for super_exception_notifier (old version, 2.0.8):
ExceptionNotifier.configure_exception_notifier do |config|
config[:sender_address] = %("Foo" <foo#foo.com>)
config[:exception_recipients] = %w(foo#foo.com)
config[:skip_local_notification] = false
end
From what I can tell, ExceptionNotifier is undefined, and ActiveSupport is trying to magically load it, but fails and then fails again trying to print a nice error message.
An anonymous module is a module that is declared like so:
Fred = Module.new do
def meth1
"hello"
end
def meth2
"bye"
end
end
instead of by using the regular Module mod_name <block> syntax. Since they have no module name, you can't retrieve the module name. to_constant_name is attempting to call desc.name.blank? where desc is an anonymous module (with no name).
This error is coming from the ActiveSupport module, which may indicate a bug in the active_support gem or may indicate that some other piece of code is using ActiveSupport incorrectly. The error message alone doesn't give enough information to identify the culprit (to me at least, someone with more rails experience might be able to provide more insight).
Without knowing the offending code it's also hard to say exactly why this error is popping up with 1.9, or what needs to be done to fix it. Considering that there are a lot of un- and under-maintained gems out there that have not been updated for 1.9 yet, I would suspect that ActiveSupport is not the source of the problem. Upgrade all of your gems that have 1.9-compatible versions, and then try disabling your other gems one at a time (if you can) and see if you still get the error.
If you provide a list of the other gems that you are using, someone else who may have encountered the error before may be able to provide some details.
This may happen if you try to exploit ActiveRecord's internal class and module contexts in the wrong way. I had this error yesterday while working on a gem which extends deep inner workings of ActiveRecord. I finally managed to get around this problem by redesigning my code which exploits the inner contexts. It would be interesting to see the surrounding lines of environment.rb:66 for further analysis.
This may happen when the class name doesn't match the filename, in
my case it was a file named application.rb contaning the ApplicationController
class. Renaming the file to application_controller.rb solved the problem.
When I got this error, it was due to a misspelling while defining a class. If you are getting this error, it may be worth examining your module and class definitions for typos.

How to be an independent Ruby programmer

I am tired of asking unanswered questions when using many outdated plugins / gems, and sometimes they don't really work how I really wanted.
So my question is simple:
If I was a PHP programmer, and Rails was my first framework, what do I need to learn next so I can depend on myself when working with troublesome plugins or code snippets or tutorials?
I used to be quite good with an (rather silly now that I know MVC) e-commerce system in PHP, and normally I would just go ahead and read out the plugin's code to find out what it does, but apparently doing so is extremely hard in Ruby on Rails, should I keep doing it, or am I going the right track? (I did learn by the way, but in very slow pace compared when I was still using PHP and that "e-commerce framework")
This is like a generic programming question for me. If you have any troubles with some Rails plugin, you can always debug its code with a bunch of hardcore or easy methods, try to understand and fix the error. The question is, which methods should you use in particular situations.
I will give you a little example. When you are debugging your Rails application, always check either logs/production.log or logs/development.log (depending on mode you're working) for errors of any kinds. Every error in Ruby/Rails is represented by a huge stack-trace that you should read from top to bottom. Like this one:
Processing CommentsController#create (for ***.***.***.171 at 2010-08-27 03:31:29) [POST]
Parameters: {"authenticity_token"=>"[STRIPPED]"}, "last_comment_id"=>"0", "original_controller"=>"projects", "thread"=>"true", "thread_id"=>"Conversation_31", "commit"=>"Save", "_"=>"", "controller"=>"comments", "action"=>"create", "conversation_id"=>"31"}
Sent mail to email#email.com
ArgumentError (invalid byte sequence in UTF-8):
/usr/lib/ruby/1.9.1/net/protocol.rb:294:in `slice!'
/usr/lib/ruby/1.9.1/net/protocol.rb:294:in `each_crlf_line'
/usr/lib/ruby/1.9.1/net/protocol.rb:236:in `write_message_0'
/usr/lib/ruby/1.9.1/net/protocol.rb:250:in `block (2 levels) in write_message'
/usr/lib/ruby/1.9.1/net/protocol.rb:280:in `using_each_crlf_line'
/usr/lib/ruby/1.9.1/net/protocol.rb:250:in `block in write_message'
/usr/lib/ruby/1.9.1/net/protocol.rb:169:in `writing'
/usr/lib/ruby/1.9.1/net/protocol.rb:249:in `write_message'
/usr/lib/ruby/1.9.1/net/smtp.rb:878:in `block in data'
/usr/lib/ruby/1.9.1/net/smtp.rb:921:in `critical'
/usr/lib/ruby/1.9.1/net/smtp.rb:875:in `data'
/usr/lib/ruby/1.9.1/net/smtp.rb:655:in `send_message'
actionmailer (2.3.8) lib/action_mailer/base.rb:684:in `block in perform_delivery_smtp'
/usr/lib/ruby/1.9.1/net/smtp.rb:526:in `start'
actionmailer (2.3.8) lib/action_mailer/base.rb:682:in `perform_delivery_smtp'
actionmailer (2.3.8) lib/action_mailer/base.rb:523:in `deliver!'
actionmailer (2.3.8) lib/action_mailer/base.rb:395:in `method_missing'
app/models/emailer.rb:89:in `send_with_language'
app/models/conversation.rb:51:in `block in notify_new_comment'
app/models/conversation.rb:47:in `each'
app/models/conversation.rb:47:in `notify_new_comment'
...
Here we are! The line ArgumentError (invalid byte sequence in UTF-8):, on the very top of this trace, always tells us the error type. It's an argument error!
After that, look at the next line: /usr/lib/ruby/1.9.1/net/protocol.rb:294:in 'slice!'. It tells where the exception (ArgumentError) was raised in the following format:
/path/to/file/:line:in `method_name'`
Let's open that neglectful file and find the source around that line:
def each_crlf_line(src)
buffer_filling(#wbuf, src) do
while line = #wbuf.slice!(/\A.*(?:\n|\r\n|\r(?!\z))/n)
yield line.chomp("\n") + "\r\n"
end
end
end
The error is on the following line:
while line = #wbuf.slice!(/\A.*(?:\n|\r\n|\r(?!\z))/n)
From this point we know where the error is and only need to fix it, but this process is out of scope of my short story. I just wanted to show you how to work with errors in Rails.
You should keep reading plugin code.
If it is extremely hard, you may become extremely good at it.
Go watch the Yehuda Katz video at on the homepage of railsconf.blip.tv. He's now one of the core rails developers and discusses how he got into it.

Resources