...or controller) logic. Here is how. Note: this has only been tested on a Rails 2 application. It should work similarly for Rails 3. Put this into your ApplicationController:
Now you can use with_full_urls in views, helpers or controllers and Rails methods like url_for will generate "full" URLs that have a protocol and hostname. You...
Next, configure your application to use that middleware by putting this inside your Rails initializer block (config/environment.rb for Rails 2, config/application.rb for Rails 3): require 'lib/rack/cookie_stripper.rb' config.middleware.use Rack::CookieStripper...
You will get this when you are using the latest version of Rails with a recent version of Rack: SECURITY WARNING: No secret option provided to Rack::Session::Cookie.
...future versions will even invalidate your existing user cookies. The warning is caused by Rails calling Rack incorrectly. It is unclear when this is going to be fixed in Rails...
There is a nasty bug in all version of Rails 2 and some versions of Rails 3.x where two chained scopes with hash conditions on the same attribute would...
...what a user may see or change. Workaround If you are using an affected Rails version and cannot switch to a fixed version, you can use this manual workaround.
You may disable multi-line autocomplete by using the --nomultiline commandline switch (for Rails, use bin/rails console -- --nomultiline), or specifying IRB.conf[:USE_MULTILINE] = false in your ~/.irbrc.
...for that. The reason why you never had to write this line is that Rails does this for you when it boots the environment. That also means that if you...
...have an embedded Rails app in your spec folder (like has_defaults), and you boot its environment, it should call Bundler.require for you and you don't need to require...
...can remove conditions, order, etc by using the unscope method. It is available on Rails 4+. Examples Consider an exemplary User class as follows. For the examples below, we will...
...scope of any constraints. users.unscoped ^ SELECT "users".* FROM "users" While unscope was introduced with Rails 4, the unscoped method has been around since Rails...
...part is a bit lengthy, but it allows you to debug strange bugs with Rails autoloading the wrong constants. Due to the constant lookup rules above, Rails sometimes guesses wrong...
...in development and production, but the problem exist for both environment. Development During development, Rails unloads all classes after every request. This way code changes take effect immediately, without requiring...
...to append filepaths to it. With this method, Ruby code can look like this: Rails.root/"features"/"fixtures"/"picture.jpg" Alternatively you can use the #join method, which feels less magic: Rails.root.join...
...and The...
...field should have the error...
...steps now have built-in support for Rails and Bootstrap (v3-v5) error classes. When using Bootstrap, it is no longer necessary to...
...performance due to the cost of reindexing. Minidusen is currently compatible with MySQL, PostgreSQL, Rails 3.2, Rails 4.2 and Rails 5.0. Basic Usage Our example will be a simple address...
...Note that #origin_class it roughly equivalent to the blockless form of #unscoped from Rails 3.2+, but it works consistently across all Rails versions. #unscoped does not exist for Rails...
...and is broken in Rails...
...behaves like an unordered Hash, but is much faster. Info If you're using Rails you already have concurrent-ruby in your bundle. hamster hamster provides several collection classes, such...
There is a practical short list for valid/invalid example email addresses - Thanks to Florian L.! The definition for valid emails...
Submit buttons in Rails come with a useful option :disable_with which will disable the button when clicked and change its label to something like "Please wait...". An annoying side...
...is to re-enable the submit button before leaving the page. This works in Rails 3: $(window).unload(function() { $.rails.enableFormElements($($.rails.formSubmitSelector...
t.column_name, :string t.remove :first_name t.remove :last_name t.rename :cool, :awesome end Rails 2 caveats Rails 2 does not understand the extra options hash but seems to build...
...only created when you rename columns. For other operations such as add and remove Rails 2 will still give you single ALTER statements. More See RobinWu's APIdock comment for...
top Also see: How to get a rough picture about a Rails project How to examine an unknown Ruby object
task :script, roles: :app, only: { primary: true } do run "cd #{current_path} && RAILS_ENV=#{rails_env} bundle exec rails runner 'ScriptRunner.go'" end becomes desc 'Run script'
on primary :app do within current_path do with rails_env: fetch(:rails_env, 'production') do execute :bundle, 'exec', "rails runner 'ScriptRunner.go'" end end end end
encoding: utf8 username: root password: secret Note if you're on an old Rails version If you're using a Rails version less than 3.1 (e.g. 2.3.x...
...Newer versions no longer include the ActiveRecord adapter (which is part of ActiveRecord since Rails .1). So your Gemfile should say:
...all styling inline, which means you'll have to do your styling inline. For Rails applications, you can use Roadie or premailer, which lets you keep your well-structured CSS...
...litmus during rendering. For a preview during develoment, you can also use your local Rails mailer previews. Take the preview HTML source and paste it to Litmus. When the builder...
...migration_user.phone ) end end end Now the best idea is to test this in the rails sandbox (so your development data stay untouched): rails console --sandbox 2.4.1 :003 > DataMigration.new.run
You have to specify the environment with -e env_name or RAILS_ENV=env_name if you want to run a script on the server. at Rails 2 it's...
bundle exec script/runner -e env_name path/to/script.rb argument1 argument2 ... at Rails 3 it's rails runner RAILS_ENV=env_name bundle exec rails runner path/to/script.rb argument1 argument2...
...add ANSI color annotations (which your console then interprets). To print a log (e.g. rails log) and color all lines containing "FATAL" in red and all lines with "INFO" in...
User.active.to_sql Rails 2 Use either the Edge Rider or fake_arel gem to get #to_sql backported to Rails 2. If you don't want to use...
...a gem for this, you can do this with vanilla Rails 2: User.active.construct_finder_sql...