Failure in Rails Tutorial (Hartl) -section 7 - "unkown attribute: password" - ruby-on-rails-3

I've searched this issue on the forum and have used a solution on the spork site to remove "--drb" from spec options, got it to load and run and still this persistent error keeps popping up. I've also run it without spork. I've read ahead on a bunch of rspec info and as stated dug through spork info, but nothing yet. I've also ran checks for typos, indentation, encoding... and am at a loss still. Other issues, I've been able to fix, but this one's got me. Here it is below if anyone has a suggestion:
Failures:
1) User password encryption has_password? method should be false if the passwords don't match
Failure/Error: #user = User.create!(#attr)
ActiveRecord::UnknownAttributeError:
unknown attribute: password
# ./spec/models/user_spec.rb:94:in `block (3 levels) in '
Finished in 0.836 seconds
1 example, 1 failure
<-- Slave(1) run done!

are you certain you have added :password as a virtual attribute on your User model? It's easy to miss but you need to include the line
attr_accessor :password

It's possible that this has changed between the time the question was asked (2011) and now, but I initially ran into the same issue -- I'd added all of the tests for password etc, and the User initialization itself was failing across the board with "unknown attribute: password".
On the face of it, GrahamJRoy's answer (and more importantly, subsequent comment of elaboration) addressed my confusion perfectly.
However, I then kept reading the next section of the tutorial and learned that the following line in the User model implies the same thing:
has_secure_password
Once I added this, I no longer needed the explicit attr_accessor declaration:
attr_accessor :password, :password_confirmation
My only complaint with the tutorial here is that Michael doesn't warn you that ALL of your User references will begin to fail if you add the :password and :password_confirmation to User.new() when the tutorial first shows you the updated constructor. I was expecting only the new tests to fail, thought I was off-track, and so searched elsewhere (such as here) for a solution before continuing the tutorial.

Make sure to change attr_accessible to attr_accessor in models/user.rb
I made the same mistake and overlooked it.

Related

Getting uninitialized constant (NameError) inside decorators when running rspec tests via rake

In my app I use an engine (blogit) to which I want to add some changes / behaviours.
I followed the guides on how to override engine controllers/models and added the following:
The Code
In config/initializer/blogit.rb
# Requires extension ruby files in lib/blogit.
Dir[Rails.root.join("lib/blogit/*.rb")].each {|f| require f}
In lib/blogit/engine.rb
module Blogit
class Engine < ::Rails::Engine
isolate_namespace Blogit
config.to_prepare do
Dir.glob(Rails.root + "app/decorators/**/blogit/*_decorator*.rb").each do |c|
require_dependency(c)
end
end
end
end
In app/decorators/controllers/blogit/comments_controller_decorator.rb
Blogit::CommentsController.class_eval do
def create
Rails.logger.info "decorated controller action"
# ... overridden stripped ...
end
end
In app/decorators/models/blogit/comment_decorator.rb
Blogit::Comment.class_eval do
belongs_to :user
end
To be mentioned:
I have also created a migration to add a user reference to the comments model, since my app uses devise and I only want logged_in users to be able to comment. (Therefore I don't need the standard behaviour, so I'm going to override it.)
The Problem
If I run rake I get the weird error:
/Users/Kassi/.rvm/rubies/ruby-1.9.3-p392-railsexpress/bin/ruby -S rspec ./spec/controllers/home_controller_spec.rb ./spec/models/user_spec.rb
/Users/Kassi/demo/app/decorators/controllers/blogit/comments_controller_decorator.rb:3:in `<top (required)>': uninitialized constant Blogit::CommentsController (NameError)
However, if I run the first line that rake mentions by hand (.../ruby -S ...), all tests are being run successfully.
In my project I'm using guard with spork. Running guard will also let the tests pass without any error.
The app itself runs fine, i.e. it starts without errors and I'm able to comment as I want. My decorator action code is being executed.
So what's different when running rake?
Why does it break?
Note:
Adding require "blogit" or require "blogit/comments_controller" doesn't help. It actually can't find the controller using require.
A Demo Application
Since this problem is part of a bigger project, I created a new app from scratch for testing purposes that contains only the relevant stuff: basic rails app, rspec, devise, blogit and the decorators.
It can be found here: https://github.com/kassi/decorator_demo_rspec (git://github.com/kassi/decorator_demo_rspec.git)
Another repo using testunit (which is working!) can be found here: https://github.com/kassi/decorator_demo_testunit (git://github.com/kassi/decorator_demo_testunit.git)
Instead of performing the require statements yourself, you can try activesupport-decorators that does it for you.

How can I have a model called Admin with ActiveAdmin?

I've tried installing ActiveAdmin into a rails app that already has an Admin model* and it all works, until I try to run any code (e.g. run specs, or run the rails server) which needs to access the Admin class -- then it fails as if the problem is with my Admin class:
foo/app/models/admin.rb:1:in `<top (required)>': Admin is not a class (TypeError)
from bar/.rvm/gems/ruby-1.9.2-p320/gems/activesupport-3.2.11/lib/active_support/dependencies.rb:251:in `require'
*In my case I used --skip-users so I could reuse my Admin model, but it happens whether you do this or not. It also happens if you do:
rails generate active_admin:install Admin
How can I get ActiveAdmin to coexist with a model (or other class) called Admin
It took me a while to work this out (on a previous project I gave in and just renamed the model).
There have been some patches proposed, but actually there is a perfectly good config option that fixes this:
Edit the initializer at config/initializers/active_admin.rb so that the default_namespace is different (you could skip the default_namespace with config.default_namespace = false, but I haven't tried this).
I chose admin_ui for this:
config.default_namespace = :admin_ui
There may be more subtlety for this so you can (if you want) keep the /admin/ route rather than have admin_ui but this was good enough for me -- the official docs on this are at http://activeadmin.info/docs/1-general-configuration.html#namespaces, but don't mention this reason to change the namespace.

Rails 3.2.8. Upgrade checkboxes from Rails 1.x.x to 3.2.8

Just was checking this episode by Ryan Bates and it seems that Rails 3.2.x has different setup.
Seems that check_box_tag requires different attributes other than Ryan puts in there. AS it writes back unexpected kEND... to <%= check_box_tag "task_ids[]", task.id %>
Any help appreciated
Your error is unrelated to your use of check_box_tag.
"unexpected kEND" indicates an unexpected end-of-file, meaning you've opened a block or other nested structure somewhere and failed to close it. Probably a do or an if with no matching end.

logging info with rails

Moving over from django / python, I am having a little trouble getting the rails logger to log all the information I want. I am wondering how/if the following can be achieved:
Having in the log format(ter) include the specific file, function name and line where the logging statement itself was found. Essentially the equivalent of LOG_MSG_FORMAT = '%(asctime)s %(levelname)s %(filename)s:%(funcName)s(%(lineno)d) : %(message)s' in python logger?
Being able to log all requests, via something similar to a django request logging middleware. Particularly, being able to log the username (if logged in) of every request.
Am I missing something obvious? or does this require (lots of) custom code?
I just found this railtie gem that might help although I imagine it will take some "custom code" to append username to logs. See the Readme section on logging specific controllers and models.
logging-rails railtie
I don't know about getting the file, function, and line number, but it's pretty easy to log from application_controller:
class ApplicationController < ActionController::Base
before_filter :log_user
def log_user
if current_user
Rails.logger.info "Processing Request for #{current_user.name}"
end
end
end
Just to add a quick note in case this is useful for someone:
The lograge gem makes rails logs much similar to django's, plus allows very neat customization, adding parameters such as remote ip address, current_user etc.
It also reduces verbosity of rendered layouts, which I anyway found unnecessary for production. It also plays nicely with logging-rails railtie mentioned by #lukewendling.
Sadly, I still couldn't find anything that shows the file/function/line number like you can easily do with django, but I guess that's just too much to ask.

Paperclip Rspec tests : why missing attr_accessor?

I had a set of Rspec User Model tests that were passing, until I had the paperclip attribute "profile picture".
Everything is fine with paperclip, it works great, but I would like my tests also to be updated to pass. I just added this line to the #attr of the user_spec file:
:avatar => File.new (Rails.root + "public/images/Default_profile_picture.jpg")
in a before_each loop that creates a standard set of working attribute for the user model.
When I run the tests, ALL the tests give me the error message:
User model missing required attr_accessor for 'avatar_file_name' ...
Putting paperclip's fields as attr_accessor in the model makes the problem disappear in the test, but I don't think I should do this in my model (it does not work properly if I do).
Does anyone has an idea why I got these error messages when testing ? ... (I included the config line Shoulda::Matchers and the require paperclip lines in the spec/helper)
Thanks for any answer you may post !
It was simply that I forgot to run
rake db:test:prepare
before running my tests :-/