I want to store the session data in database using the ActiveRecord::SessionStore module. I have been searching for quite sometime for this without success. Either i am not using the proper search terms or i am being blind to something very obvious.
I have used this statement require ActiveRecord::SessionStore::Session in my code to enable session handling with active record. It conks out with the error uninitialized constant ActiveRecord::ActionDispatch . I assume that i have install the actiondispatch module. Am I correct?
Please bear in mind that this is my first shot with Ruby-Sinatra. I am coming from PHP.
So, what should i use to make Sinatra use database-based sessions using ActiveRecord?
I ended up solving the uninitialized constant ActiveRecord::ActionDispatch error by adding the actionpack gem to my application's Gemfile and requiring action_dispatch. You might also have to require 'logger'
Sinatra is a Rack based application and allows addition of middleware. Middlewares can be plugged into any Rack based framework.
Search on references of config.ru in the Sinatra Book
so you would create a config.ru file in the root of your app.
and put something like this
require 'my_app'
use ActiveRecord::SessionStore
run MyApp
Related
I have a Rails 4.2.8 app, ruby 2.4.4 with a Mongo database, using the mongoid ODB.
I want to run tests against this application using rspec. The most basic test gives me this error:
ActiveRecord::ConnectionNotEstablished: No connection pool for ActiveRecord::Base
My research on this problem lead me to these conclusions, but I do not know how to solve the problem itself:
why is active record mentionned here, when it is not in my gemfile ? Maybe another gem is loading it at some point, but how can i get rid of this problem ?
my application.yml and mongoid.yml file are correctly configured. I can RAILS_ENV=TEST rails console and I can query documents.
looks like rails is going inside the condition line 15 in this file. So how and why is ActiveRecord loaded at some point ?
Some help would really be appreciated as I do not know what to do next...
Ok so I did not figure out who was loading ActiveRecord in my application, but I manually unload it in my rails_helper.rb:
Object.send(:remove_const, :ActiveRecord)
# Make sure it is above this
require 'rspec/rails'
I ran into the same issue and found that the problem was database cleaner. Previously we were using the gem database_cleaner but that gem seems to have been split based on your database adapter. Switching to database_cleaner-mongoid resolved the issue. Note that you may need to change some requires or config values to get things running correctly after the change. For instance I had an error about my strategy not being supported in mongoid.
I'm trying to get a better handle on Rack middleware, and I've just learned that you can view all of the installed middlewares for a Rails app using rails middleware. In a fresh Rails app, that's about 25 different middlewares.
I'd really like to examine the source code of these different middlewares, but I can't find out where they live in the app. Have they all been compiled to executable, so the only way I can read the source is on Github? If so, how can I find those repos? Thanks!
For the ActionDispatch, ActiveRecord, Rails, ActiveSupport, and Testing middlewares, they'll be in rails/rails, the Rack stuff will be in rack/rack, and WebConsole will be in rails/web-console
They're not compiled into an executable, no. Each gem is downloaded based on the contents of your Gemfile and gets semi-magically loaded via your environment and config.ru.
Situation
I am currently testing a Rails app using Capybara. In addition, I was using Guard with its extension guard-livereload to automatically reload my browser as soon as relevant source files changed.
As the save_and_open_page method from Capybara did not display stylesheets correctly, I applied this solution to the problem, in which a temporary view-dump capybara.html gets placed in the /public/ folder to ensure the accessibility of assets.
Now, as LiveReload has worked like a charm in development, I would like to use it during feature-tests to automatically reload /public/capybara.html instead of opening it over and over myself.
Problem
For some reason I can only insert the Rack Middleware, which is responsible for reloading the page, into the middleware-stack within the developent-environment, but not within the test-environment. I use the following code for insertion:
/config/environments/development.rb
Rails.application.configure do
config.middleware.insert_after ActionDispatch::Static, Rack::LiveReload
end
When using the same method in /config/environments/test.rb, the following error occurs
myApp/config/environments/test.rb:44:in `block in <top (required)>': uninitialized constant Rack::LiveReload (NameError)
As I am still rather new to Rails, I don't really know where to start here. As far as I know, trying to require the file manually wouldn't really be The Rails WayTM .
So, how can I resolve this problem?
Thanks in advance.
In your Gemfile you are probably only loading the rack-livereload gem in the development group -- for this to work you would need to be loading it in the development and test groups. That being said, you really want your test environment to mimic production as closely as possible, so running rack-livereload in the test environment would usually be a bad idea.
I believe you should include the Rack::LiveReload in your test environment inside Gemfile:
group :development, :test do
gem "rack-livereload"
end
I have a Sinatra app and I'd like to start building new functionality in Rails while still supporting the existing Sinatra functionality. I've tried the following strategies:
sinatra's rackup routes some requests to rails and some to sinatra
sinatra's rackup includes rails
rails' rackup includes sinatra.
Many of my searches resulted in rails 3, not 4. Additionally, does Rails have to generate the db versus using one that Sinatra was using (in this case, the Sequel gem to access Sqlite3.) In general the errors I was getting were about gems and paths. (Although I did rebundle and try different versions of paths.)
Any suggestions on the best way to use Rails 4 while still supporting an existing Sinatra app?
I don't think the Rails/Rack integration code has changed very much between Rails 3 and 4, so you should be fine. The Rails on Rack Guide explains in more detail that you can make a config.ru file for a Rails application that looks like:
require ::File.expand_path('../config/environment', __FILE__)
use Rack::Debugger
use Rack::ContentLength
run Rails.application
and then running rackup config.ru will start a rack server running your rails app.
The answers to this question point out that if you run Rails and Sinatra from Rack, rather than mounting your Sinatra app in Rails' routes.rb file, requests to your Sinatra app won't go through Rails at all. The answers also show that in your config.ru you should be able to do this to support both your Sinatra and Rails apps:
map "/" do
run RailsApp::Application
end
map "/url1" do
run SinatraApp1
end
You'll have to modify the routes and application names to match your needs and your applications, of course.
I'd recommend getting your apps running through one config.ru first, and then ask another question about your databases, explaining in more detail what you'd like the database setup to be and what the exact error messages you're getting are.
Rails does not need to create a database or even use one directly. To generate a new Rails app without ActiveRecord, run rails new APP_PATH --skip-active-record. Then, instead of using a database directly from the Rails app, send requests to the Sinatra app and have the Sinatra app control everything database-related.
I've got a Rails project that's setup using Bundler. One of my bundled gems provides a Rack middleware that I'd like to use in my Rails app (but only in the 'production' Rails environment).
If I just put something like this in config/environments/production.rb, I get an unknown constant error:
config.middleware.use ::Rack::MyMiddleware
... presumably because Bundler.require has not yet been called at this point, and none of my bundled gems are available.
I have found a few ways of working around this, but none seem great, and I'm wondering if there's a more standard solution that I'm missing:
Explicitly require 'my_middleware_gem' in config/environments/production.rb
Wrap the config.middleware.use call in an after_initialize block, ensuring that Bundler has a chance to do its thing before I try to reference the constant.
Use a string ("::Rack::MyMiddleware") instead of the bare class constant. This doesn't seem to work with Unicorn for some reason, but does work with some other servers (with Unicorn it ends up trying to call "::Rack::MyMiddleware".new, which of course fails).
Am I missing some better approach? At what point in the initialization process is it expected that bundled gems should be available?
Copying the answer from the comments in order to remove this question from the "Unanswered" filter:
matt suggested:
I think using the after_initialize block is the right way in this case.
grumbler confirmed:
Yeah, that's what I ended up going with. Thanks! Regarding the unicorn issue alluded to in the original question, turns out I was hitting this problem: http://davidvollbracht.com/blog/headachenewunicorn-capistrano-bundler-usr2