Accessing UserManager outside AccountController

If you’re using the default project template, the UserManager gets created the following way: In the Startup.Auth.cs file, there’s a line like this: app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create); that makes OWIN pipeline instantiate an instance of ApplicationUserManager each time a request arrives at the server. You can get that instance from OWIN pipeline using the following code inside a … Read more

Threading error when using `ActiveRecord with_connection do` & ActionController::Live

Warning: read ‘answer’ as ‘seems to make a difference’ I don’t see the issue happen if I change the controller block to look like: begin #… while true do t = Thread.new do #<<<<<<<<<<<<<<<<< ActiveRecord::Base.connection_pool.with_connection do |conn| #… end end t.join #<<<<<<<<<<<<<<<<< end #… rescue IOError #… But I don’t know whether this has actually … Read more

Does Rails come with a “not authorized” exception?

Rails doesn’t seem to map an exception to :unauthorized. The default mappings are defined in activerecord/lib/active_record/railtie.rb: config.action_dispatch.rescue_responses.merge!( ‘ActiveRecord::RecordNotFound’ => :not_found, ‘ActiveRecord::StaleObjectError’ => :conflict, ‘ActiveRecord::RecordInvalid’ => :unprocessable_entity, ‘ActiveRecord::RecordNotSaved’ => :unprocessable_entity ) and actionpack/lib/action_dispatch/middleware/exception_wrapper.rb: @@rescue_responses.merge!( ‘ActionController::RoutingError’ => :not_found, ‘AbstractController::ActionNotFound’ => :not_found, ‘ActionController::MethodNotAllowed’ => :method_not_allowed, ‘ActionController::UnknownHttpMethod’ => :method_not_allowed, ‘ActionController::NotImplemented’ => :not_implemented, ‘ActionController::UnknownFormat’ => :not_acceptable, ‘ActionController::InvalidAuthenticityToken’ => :unprocessable_entity, ‘ActionDispatch::ParamsParser::ParseError’ … Read more

how to handle exceptions in JSON based RESTful code?

(I found the answer just before I hit [Post your question]. But this might help someone else as well…) Use ActionController’s rescue_from The answer is to use ActionController’s rescue_from, as described in this Guide and documented here. In particular, you can replace the default rendering of the default 404.html and 500.html files along these lines: … Read more