Session not getting initialized - ruby-on-rails

I've been trying for the last 4 days to understand what has happened so that session isn't getting initialized anymore.
My app was working just fine, until one day suddenly i started having the error
undefined local variable or method `session' for < StaticPagesController:0x7c84728 >
I debugged it and tracked it down to request_forgery_protection.rb file, under action_controller\metal.
def form_authenticity_token
session[:_csrf_token] ||= SecureRandom.base64(32)
end
So, apparently, session wasn't being loaded.
I then tried suggestions from How force that session is loaded?, as you may see below.
def root
if signed_in?
...
else
session[:init] = true
session[:init]
#prospect = Prospect.new()
render 'retailers/retailers_home'
end
end
but still the same error keeps showing up.
Under my intializers, session_store.rb, everything seems fine :
RecibosOnline::Application.config.session_store :cookie_store, key: '_RecibosOnline_session'
as this exact code works on other developer's machine, as well as on other server.
So this must be something specific to my machine...but why??
What might be causing this?

When all else fails and nothing makes sense anymore, reset the world:
git clean -fdx

Related

Error attempt to index a nil value (upvalue "cfg)

I was moving some map files, and I rebooted my server (FiveM) and when I called it again it started to give this problem, I undo everything I did, I put the old files again, but the error kept happening. And I do not know what to do to solve it.
I threw all the changed files back to normal, but it did not solve anything.
local cfg = module("cfg/blips_markers")
-- add additional static blips/markers
AddEventHandler("vRP:playerSpawn",function(user_id, source, first_spawn)
if first_spawn then
for k,v in pairs(cfg.blips) do
vRPclient.addBlip(source,{v[1],v[2],v[3],v[4],v[5],v[6]})
end
for k,v in pairs(cfg.markers) do
vRPclient.addMarker(source,{v[1],v[2],v[3],v[4],v[5],v[6],v[7],v[8],v[9],v[10],v[11]})
end
end
end)
Inside the server the blip_markes are gone and some player data too, because of that.
A quick debug would be to assign cfg the value of an integer, if your problem solves, you know that something is wrong with module("cfg/blips_markers"). However, please provide the code associated with module("cfg/blips_markers").

RoR - Validate string on website, no error

In my Ruby on Rails app I have build a function to validate if a piece of javascript is added to a certain website. When I run this code I don't get any errors in my log, but my app says:
We're sorry, but something went wrong.
If you are the application owner check the logs for more information.
But when I check the logs I don't see any errors. The code I have used is the following:
def validate_installation
data = HTTParty.get(self.website)
url = "http://www.smartnotif.com/sn.js"
if data.body.include? url
return true
else
return false
end
end
When I run this code on my local development machine it runs fine, but when I try to runs this production machine on DigitalOcean I have this problem with the same code, no errors.
Try to include
require 'httparty'
Restart rails server
rails s
Also check the permission of log folder, why it is not writing error in log folder
Also try: Use self keyword as you are calling it as class method
def self.validate_installation
data = HTTParty.get(self.website)
url = "http://www.smartnotif.com/sn.js"
if data.body.include? url
return true
else
return false
end
end

Stubbing instance method for OpenTok object

The gem I'm using to integrate OpenTok in my Rails application is at: https://github.com/opentok/Opentok-Ruby-SDK. I based the core of the application on this example: http://www.tokbox.com/blog/building-a-video-party-app-with-ruby-on-rails.
In the relevant part of code, I'm creating an #opentok object in the config_opentok method:
def config_opentok
if #api_key.nil? or #api_secret.nil?
if Rails.env.development?
#api_key = API_KEY
#api_secret = API_SECRET
else
#api_key = ENV['API_KEY']
#api_secret = ENV['API_SECRET']
end
end
if #opentok.nil?
#opentok = OpenTok::OpenTokSDK.new(#api_key, #api_secret)
end
end
And I'm creating a session with the following code:
config_opentok
if Rails.env.development?
session = #opentok.create_session('localhost')
else
session = #opentok.create_session(request.remote_addr)
end
The trouble is, the create_session seems to throw an error
SocketError: getaddrinfo: nodename nor servname provided, or not known
whenever I run my Rspec tests without an internet connection. So I'd like to stub that method so that it returns just a hash {:sessionId => 1}. But I'm having trouble figuring out how to stub the method. I can't just stub the OpenTok module or the OpenTok::OpenTokSDK class. How would I go about stubbing the create_session method?
here's what I've been doing that works:
First, what I tend to do is to initialize the OpenTok object when the app loads so I'm not creating an OpenTok object on every request. To do this, I create a ruby file (apis.rb) in my config/initializers folder.
My apis.rb looks like this:
TB_KEY = ENV['TB_KEY']
TB_SECRET = ENV['TB_SECRET']
OTSDK = OpenTok::OpenTokSDK.new TB_KEY, TB_SECRET
In my controller, to generate a session I'll simply call OTSDK.createSession, similar to what you already have.
To test with rspec, you can simply write in your test file:
OTSDK.stub(:createSession).and_return( {:sessionId => "1MX_2A3453095J0TJ30..."} )
If you run rspec with wifi turned off calling createSession should no longer throw an error.
Here's the documentation for rspec stubbing: http://rubydoc.info/gems/rspec-mocks/frames
Good Luck!
The trouble is, the create_session seems to throw an error whenever I run my Rspec tests without an internet connection.
Instead of attempting to stub, why not give your tests a mock internet connection with VCR?
After initial set up, VCR lets you run all of your tests as if you were actively connected to the internet. This allows you to run tests offline, speeds up all the tests that needed an active connection, and gives you a consistent set of results.
If you have a subscription to RailsCasts, Ryan made a video about VCR in episode 291

Resource temporarily unavailable (Timeout::Error) cucumber visit only on jenkins

call to visit some_path fails consistently on ubuntu / jenkins with Resource temporarily unavailable (Timeout::Error), but passes consistently locally on mac. Tried every suggestion I could find online... no luck. Anyone ever run into this?
This is only thing that worked. I tried Around hooks... nothing. M-I-C K-E-Y M-O-U-S-E...
def short_bus_visit( url )
10.times do |i|
begin
visit url
break
rescue Exception
next
end
end
end

Ruby timeout does not work in Rails?

I'm having an issue trying to get a timeout when connecting via TCPSocket to a remote resource that isn't available. It just hangs indefinitely without timing out. Ideally I'd want it to try reconnect every 2 minutes or so, but the TCPSocket.new call seems to block. I've tried using timeout() but that doesn't do anything either. Trying the same call in an IRB instance works perfectly fine, but when it's in Rails, it fails. Anyone have a work around for this?
My code looks something as follows:
def self.connect!
##connection = TCPSocket.new IP, 4449
end
def self.send(cmd)
puts "send "
unless ##connection
self.connect!
end
loop do
begin
##connection.puts(cmd)
return
rescue IOError
sleep(self.get_reconnect_delay)
self.connect!
end
end
end
Unfortunately, there is currently no way to set timeouts on TCPSocket directly.
See http://bugs.ruby-lang.org/issues/5101 for the feature request. You will have use the basic Socket class and set socket options.

Resources