Why do I get "uninitialized constant ApplicationController::SessionsHelper (NameError)"? - ruby

I'm doing Michael Hart's tutorial and I get the error:
rails_projects/sample_app/app/controllers/application_controller.rb:3:in `<class:ApplicationController>': uninitialized constant ApplicationController::SessionsHelper (NameError)
Here is my application_controller.rb file:
class ApplicationController < ActionController::Base
protect_from_forgery
include SessionsHelper
# Force signout to prevent CSRF attacks
def handle_unverified_request
sign_out
super
end
end

You should have a file in app/helpers named "sessions_helper.rb". Inside of that you should at least have code like:
module SessionsHelper
end
I hope that helps.

Not sure if you got the answer yet, but I was able to comment the sessionhelper line out and get mine to work. I don't know if this has any far-reaching ramifications, but it help me circumvent the issue for now.

Where did you define SessionHelper? If it's at the top level module, try this:
include ::SessionHelper

Change include SessionsHelper to include SessionHelper
remove S

I had this same issue. Make sure you have run the migration once deployed to heroku:
heroku run rake db:migrate

Related

Not able to call method in a gem

This might be an easy question but I was unfortunately not able to find the answer on Google.
Context:
I am working on a project of my own, and I am externalizing some code in a gem (FrenchTaxSystem). It is the first I create a gem and I have difficulties using it properly.
Problem:
When calling a method (like testit) defined in the main file (french_tax_system.rb) of my gem I get a "NoMethodError: undefined method `testit' for FrenchTaxSystem:Module", though I can call constants from this same file (like FISCAL_NB_PARTS_FOR_MARRIED_COUPLE) and it puzzles me.
E.g in IRB I get that when calling a method:
[
And it is the same in my Rspecs tests inside my gem
However when calling a constant I have no error:
Main file in my gem:
french_tax_system.rb
module FrenchTaxSystem
class Error < StandardError; end
# Constants
...
FISCAL_NB_PARTS_FOR_MARRIED_COUPLE = 2
...
# Methods
## Main method
def testit
"test me"
end
end
Gem file structure:
Thank you in advance for your help,
Mth0158
This should work:
module FrenchTaxSystem
def self.testit
"test me"
end
end

uninitialized constant User::Permissions (NameError)

I am in the process of learning how to use Classes, Modules and Namespaces. I am doing some playground stuff by myself and created a user with some permissions.
User.rb
class User
include Permissions
end
u = User.new.set_permissions
Permissions.rb
module Permissions
def set_permissions
p 'Settings permissions'
end
end
I am currently getting this error when I run this User.rb file.
uninitialized constant User::Permissions (NameError)
Why would this be happening? I tried include and require but saw the same error. They are two serpeate files within the same folder. There are no subfolders like lib or anything in this directory.
You need this at the top of user.rb:
require_relative 'permissions'
This will load the code from permissions.rb so that when you call include Permissions, the Permissions module has been defined.
What you can do in that specific case is,
require './permission'
class User
include Permission
def permit
set_permission
end
end
then you can easily call permit on newly created User instance.
User.new.permit
this will let you achieve load and run a module function.

Ruby on Rails Uninitialized constant SomeModule::SomeClass

I have this class under lib/some_module in my project:
module SomeModule
class SomeClass
def initialize
end
end
end
When I go into rails console and I type in SomeModule::SomeClass.new, it works just finel. But when I start up the server and try and access it from some other class, I get the error:
uninitialized constant SomeModule::SomeClass
I have added lib to my autoload in Application.rb. Not sure what might be going wrong
You need to add wildcard path to autoload (I'm not sure why - this fixed this error on my machine).
Therefore, add to application.rb:
config.autoload_paths += Dir["#{config.root}/lib/**/"]

How to define a method to be called from the configure block of a modular sinatra application?

I have a Sinatra app that, boiled down, looks basically like this:
class MyApp < Sinatra::Base
configure :production do
myConfigVar = read_config_file()
end
configure :development do
myConfigVar = read_config_file()
end
def read_config_file()
# interpret a config file
end
end
Unfortunately, this doesn't work. I get undefined method read_config_file for MyApp:Class (NoMethodError)
The logic in read_config_file is non-trivial, so I don't want to duplicate in both. How can I define a method that can be called from both my configuration blocks? Or am I just approaching this problem in entirely the wrong way?
It seems the configure block is executed as the file is read. You simply need to move the definition of your method before the configure block, and convert it to a class method:
class MyApp < Sinatra::Base
def self.read_config_file()
# interpret a config file
end
configure :production do
myConfigVar = self.read_config_file()
end
configure :development do
myConfigVar = self.read_config_file()
end
end
Your configure blocks are run when the class definition is evaluated. So, the context is the class itself, not an instance. So, you need a class method, not an instance method.
def self.read_config_file
That should work. Haven't tested though. ;)

How do I change the aws-ruby log location?

I've found the method set_log in the documentation, I just can't figure out the syntax to call it. Here's what I tried:
require 'ruby-aws'
Amazon::Util::Logging.set_log('my.log')
NoMethodError: undefined method `set_log' for Amazon::Util::Logging:Module
You can see that Amazon::Util::Logging is a module and set_log is a 'Public Instance method'. So you need
class NewClass
include Amazon::Util::Logging
def foo
set_log('file.txt')
log 'debug_message'
end
end
I ran into this problem when trying to deploy a Ruby-on-Rails site that uses 'aws-ruby' to heroku (I got the "Permission denied - ruby-aws.log" error).
To change the log file location from 'ruby-aws.log' to 'log/ruby-aws.log', I added the following to an initializer. Make sure this is called before you use any of the aws-ruby library. Notice the change on the "set_log..." line.
module Amazon
module Util
module Logging
def log( str )
set_log 'log/ruby-aws.log' if ##AmazonLogger.nil?
##AmazonLogger.debug str
end
end
end
end
A simpler way would be to add this line:
set_log("/dev/null")

Resources