Can't create users in rails when using authlogic because of cookies - ruby-on-rails

So, I have a standard rails 3 app with authlogic. If I'm not in the browser (in the console or in the test environment), I can't create user models.
For example:
I have this code either in a rspec test or in my console:
user = User.create(...user attributes...)
And I get this error:
NoMethodError: undefined method `cookies' for main:Object
I've looked all over the internet and can't figure this out. Any help would be greatly appreciated.
EDIT:
So, I looked around some more and found in the documentation to do this:
include Authlogic::TestCase
but then I get this error:
uninitialized constant Authlogic::TestCase

I had this same exact problem. Where in your application did you put the following line:
Authlogic::Session::Base.controller = Authlogic::ControllerAdapters::RailsAdapter.new(self)
I ran into this error when I placed the line in either the environment.rb file or within a file within my initializer folder.
I eliminated the problem when I placed the line in my ApplicationController instead.

Related

Namespace module class methods undefined

I'm trying to use modules for namespacing reasons. I have this file located in my Rails app at /lib/reports/stripe.rb.
module Reports
module Stripe
def self.foo
puts 'i am foo'
end
end
end
In my console, I'd expect to be able to call foo by Reports::Stripe.foo or Reports::Stripe::foo, but I get the error
NoMethodError: undefined method `foo' for Reports::Stripe:Module
What am I doing wrong? Also feel free to let me know if there's a better way to organize the location and namespacing.
All method calls in ruby use the . syntax. Even "module" methods.
> Reports::Stripe.foo
i am foo
You may be receiving the error NoMethodError: undefined method 'foo' for Reports::Stripe:Module if you have added the method after you have started the rails console. Try restarting the console or reloading the file with load 'reports/stripe'.
The file was actually located in /lib/reports/stripe/stripe.rb. It was a mistake I made much earlier, but forgot to fix. Moving the file to /lib/reports/stripe.rb resolved the issue.

Log full stacktrace in rails including all gems

Currently when I look in the log files of my rails application I get stacktraces like:
NoMethodError (undefined method `[]' for nil:NilClass):
app/controllers/concerns/example.rb:192:in `rescue in create_example'
app/controllers/concerns/example.rb:163:in `create_example'
app/controllers/concerns/example.rb:11:in `example'
app/controllers/example_controller.rb:39:in `create'
The error is triggered from a second project which is included as gem.
On line 192 in the example.rb (concern) we use some classes from that included gem and in that class the real exception occurs.
Another example:
ZeroDivisionError (divided by 0):
app/controllers/dummy_controller.rb:15:in `index'
And on line 15
test_object.divide_by_zero
test_object is an instance of a class defined in the included gem
I want rails to display and log the full stacktrace including all or specific gems but I can't figure out how to do this. Does someone know how to do this? or someone that can give me a push in the right direction?
Thanks!!!
It's used like this:
Rails.backtrace_cleaner.remove_silencers!
Depending on what version of rails you are using this may help:
http://api.rubyonrails.org/classes/ActiveSupport/BacktraceCleaner.html#method-i-remove_silencers-21

Conflict between Rails Admin and Impressionist gems

I'm using the latest version of the Impressionist and Rails Admin gems, and wondering if anyone could shed some light on an annoying conflict I'm experiencing. The problem is roughly documented here - https://github.com/sferik/rails_admin/issues/1315, yet the vaguely described solution is not working for me. When I have the line is_impressionable in my Listing model, I get an error when starting my Rails server with rails s:
...rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.2/lib/active_record/dynamic_matchers.rb:22:in `method_missing': undefined local variable or method `is_impressionable' for Listing(no database connection):Class (NameError)
If I first start the server, and then add the 'is_impressionable' line, everything works fine, so the problem only occurs during initialization. I don't fully understand the initialization process, so am not sure how to go about getting this to work.
I have tried moving all my rails_admin model configuration options to their respective models, rather than in the initializer, which had no effect. I also have the following line in my initializer:
config.included_models = [Listing,ListingImage,AllOtherModelsHere...]
I have tried adding single quotes around these model names, which results in the following errors, as described in the github issue here
[RailsAdmin] Could not load model Listing, assuming model is non existing. (undefined local variable or method `is_impressionable' for Listing(no database connection):Class)
Any ideas what else I can try to make these gems work together? I don't want to have to remove the is_impressionable line every time I want to restart the server or generate a migration...
Not sure if the same issue that I had but yet I will post what worked for me just in case someone struggles with this too:
Im on a ruby 2.1.5 project with rails 4.2.0 and among other gems I'm using rails admin.
I run into several weird problems trying to set this up. For instance if I added the is_impressionable call within one of my models for some reason the execution of that file stopped there and I started getting weird errors like any method declared below the is_impressionable failed with undefined error.
So what I end up doing was:
class MyModel < ActiveRecord::Base
include Impressionist::IsImpressionable
is_impressionable
end
So this solved my issue and now i can access #my_model_instance.impression_count as expected.
I changed every occurrence of Klass to 'Klass'.constantize in initializer.

resque-web: NoMethodError at /failed undefined method `to_yaml'

Resque-web is up and running but when trying to view the failed jobs I get the following error:
NoMethodError at /failed
undefined method `to_yaml' for 3:Fixnum
The stack trace points to a 'to_yaml' call as mentioned in the error, it seems like resque-web is missing a requires. Has anyone else had this problem or know how to solve it?
Note: I'm running this locally on a rails 4 app.
You can see the reason here: https://github.com/resque/resque/issues/1143 -- it boils down to a temporary bug that is fixed, but not released yet. I fixed mine by adding
require 'yaml'
at the top of the server.rb file
note that the server.rb file on my machine was located at:
/usr/local/lib/ruby/gems/2.0.0/gems/resque-1.25.1/lib/resque/server.rb
your location may vary

NoMethodError when creating new user in rails

So when I submit all the info to create a new user: i get this error:
NoMethodError in UsersController#create
undefined method `sign_in' for #
I googled for it first and found answers where I was supposed to create a sessions_helper.rb file and include it in application.rb , did it all and when I refreshed after each step, it seemed to work. But now when I reloaded the page from the start again, I got the same error again.
Veske showed me his code: https://gist.github.com/Veske/7536022
You have to define your method in the SessionHelper, it is not going to automatically be made for you :)
You need to create a sign_in method in at least the SessionsHelper class.
If you'd like to use the devise gem for users creation/authentication, you'll have that method available in your code directly.

Resources