Using ActiveRecord's #signed_id and .find_signed methods you can create URLs that expire after some time. No conditionals...
...writing a piece of reusable code, you sometimes need to have separate code for Rails 2 and Rails 3. You can distinguish between Rails versions like this: if Rails.version...
...mind the quotes # Rails 2 code goes here else # Rails 3+ code goes here
If a model inherits from others or uses many concerns / traits, it might be hard to see in the code...
Instead of cobbling together default settings in several different places as the issues arise, you can define them application-wide...
...capable of caching images. (FYI, here's how to do the upload all manually.) Rails setup Build these models: class Gallery < ActiveRecord::Base has_many :images, dependent: :destroy accepts_nested...
...connect a record to an already uploaded file = image_form.hidden_field :file_cache # Used by Rails to distinguish new from existing records = image_form.hidden_field :id # Used by Rails to know when...
The information in this card is only relevant for Rails 2.3-era apps. This note gives a quick introduction into caching methods (page caching, action caching and fragment caching) in...
...rails and describes some specific problems and solutions. The descriptions below are valid for Rails 2 and 3. Recently, caching with timestamp- or content-based keys has become more popular...
...decide to open a file directly instead of having to save it first. For Rails >= 3.2 Simply put your mime types in config/initializers/mime_types.rb. send_file will take care of everything...
For Rails < 3.2 Put your mime types in config/initializers/mime_types.rb. Additionally, tell send_file to use them (for example like this): file = @attachment.file.path file_extension = File.extname(file)[1..-1]
Rails uses a CSRF token in forms and AJAX requests to verify a user request. Internally it compares the injected CSRF token of the form data with the CSRF token...
...the CSRF token from the form data. Here is a method that works for Rails 5.2. def decode_csrf_token(csrf_token) decoded_masked_token = Base64.strict_decode64(csrf_token)
Specify these gem versions in your Gemfile: gem 'cucumber', '~> 1.3.0' gem 'cucumber-rails', '= 0.3.2' # max version for Rails 2 gem 'capybara', '< 2' # capybara 2+ requires Rails 3 gem 'mime-types...
Any 404 error that is caused by something not found in your Rails app will render a nice message using your default layout. Similarly, you may catch and...
If you need to log to a file you can use Ruby's Logger class: require 'logger'
...and install RubyGems yourself. Install Bundler sudo gem install bundler Put bundler on a Rails 2.3 project Follow the instructions on bundler.io You should have patched your boot.rb
...reads Gemfile and builds Gemfile.lock with all dependencies etc. If you see "Missing the Rails 2.3.8 gem." you forgot to bundle Rails. If you see "Missing the mysql gem", bundle...
The Rails logger will store its content in a buffer and write it into the file system every 1000 lines. This will come back to bite you when using Rails.logger.info...
...environments auto_flushing is set to write after each line. On production environments the Rails logger writes only every 1000 lines -- and not upon shell or script termination as you...
...the same routes, the controller paths will be different, though. In the first example rails will expect to find a Users::SignUpsController in the subdirectory controllers/users/sign_ups_controller.rb, but there has to...
...first example of this card (resource :report, only: :show, controller: 'projects/report'). Solution for legacy Rails versions What you can do instead is simple re-use the :projects symbol as both...
...row becomes expensive The linked article shows how to create a partial index with Rails...
...prevent this, test for the right environment and place the JS-code afterwards: - if Rails.env.production? :javascript var _gaq = _gaq || []; _gaq.push(['_setAccount', 'UA-XXXXXXXX-X']); _gaq.push(['_trackPageview...
See our new comprehensive guide for upgrading every Rails 2 version ever...
See our new comprehensive guide to upgrading every Rails 2 version ever...
...presenter may keep them from reinventing the app logics-wheel, but feels like reinventing the Rails-wheel. (Arne) Bad: Duplicate Validations are bound to be problematic. This could have been...
If you get an error like this ... can’t find executable rails for rails-3.2.3 (Gem::Exception) ... one of several things might be wrong. You're using RVM
...possible that you have a system-wide gem executable (like rails) that requires a gem that is not available in the current RVM Ruby or gemset. Check what Ruby you...
Yesterday, Rails fixed a security issue (CVE-2014-3514) in Rails 4+. It was possible to use .where or .create_with to bypass Rails' Strong Parameters: user.blog_posts.create_with...
...know .create_with existed, have a look at the API. How this applies to Rails 3 In Rails 3, Strong Parameters did not exist, yet. Instead, you used mass assignment...
Note that this seems to affect only recent Rails 2 versions. You will not encounter this until you are writing to the cookie more than once, but when doing so...
Solution This seems to be a bug in Rails, the CookieStore (for sessions) or Rack::Utils.set_cookie_header -- I did not bother looking closer. My problems went away...
Rails flashes (FlashHash) track a list of used keys, which is not respected when comparing flash hashes. This does not concern you under most circumstances. Basics When ActionController picks up...
...method which checks for that: ActionController::Flash::FlashHash.class_eval do # This is from a Rails 2 project. For Rails 3, say: # ActionDispatch::Flash::FlashHash.class_eval do def same?(other_flash...
You may encounter problems with passenger starting an application with an updated rails. If you find an error like this in the apache error log...
...17680/7f4907ae7700 agents/HelperAgent/RequestHandler.h:2210 ]: [Client 19] Cannot checkout session. Error page: This application is a Rails 3 application, but it was wrongly detected as a Rails 1 or Rails 2 application...