Rails.env vs RAILS_ENV

According to the docs, #Rails.env wraps RAILS_ENV: # File vendor/rails/railties/lib/initializer.rb, line 55 def env @_env ||= ActiveSupport::StringInquirer.new(RAILS_ENV) end But, look at specifically how it’s wrapped, using ActiveSupport::StringInquirer: Wrapping a string in this class gives you a prettier way to test for equality. The value returned by Rails.env is wrapped in a StringInquirer object so instead … Read more

Why does adding “sleep 1” in an after hook cause this Rspec/Capybara test to pass?

I suspect that it’s possible in your application for the comment to disappear from the page (which is the last thing you’re asserting) before it’s deleted from the database. That means that the test can clean up before the deletion happens. If this is the case you can fix it by waiting for the actual … Read more

rails many to many self join

I’d use something like class Person < ActiveRecord::Base has_many :friendships, :foreign_key => “person_id”, :class_name => “Friendship” has_many :friends, :through => :friendships end class Friendship < ActiveRecord::Base belongs_to :person, :foreign_key => “person_id”, :class_name => “Person” belongs_to :friend, :foreign_key => “friend_id”, :class_name => “Person” end And the tables would be like people: id; name; whatever-you-need friendships: id; … Read more

Where to put Global variables in Rails 3

If you have already tried restarting your server as Ryan suggested, try putting it in your application.rb like this: module MyAppName class Application < Rails::Application YOUR_GLOBAL_VAR = “test” end end Then you can call it with the namespace in your controllers, views or wherever.. MyAppName::Application::YOUR_GLOBAL_VAR Another alternative would be using something like settingslogic. With settingslogic, … Read more

ActionController::InvalidAuthenticityToken in RegistrationsController#create

Per the comments in the core application_controller.rb, set protect_from_forgery to the following: protect_from_forgery with: :null_session Alternatively, per the docs, simply declaring protect_from_forgery without a :with argument will utilize :null_session by default: protect_from_forgery # Same as above UPDATE: This seems to be a documented bug in the behavior of Devise. The author of Devise suggests disabling … Read more

Rails 4.0 with Devise. Nested attributes Unpermited parameters

config/routes.rb Create your own registration controller like so … (see Devise documentation for the details of overriding controllers here …) … which is more elegant way as opposed to doing it via the ApplicationController devise_for :users, controllers: {registrations: ‘users/registrations’} app/controllers/users/registrations_controller.rb Override the new method to create a Profile associated with the User model as below … Read more