Rails 4: before_filter vs. before_action
As we can see in ActionController::Base, before_action is just a new syntax for before_filter. However the before_filter syntax is deprecated in Rails 5.0 and will be removed in Rails 5.1
As we can see in ActionController::Base, before_action is just a new syntax for before_filter. However the before_filter syntax is deprecated in Rails 5.0 and will be removed in Rails 5.1
Rails 4.x When you already have users and uploads tables and wish to add a new relationship between them. All you need to do is: just generate a migration using the following command: rails g migration AddUserToUploads user:references Which will create a migration file as: class AddUserToUploads < ActiveRecord::Migration def change add_reference :uploads, :user, index: … Read more
Important: Make sure your app is not using I18n 0.6.8, it has a bug that prevents the configuration to be set correctly. Short answer In order to silence the warning edit the application.rb file and include the following line inside the Rails::Application body config.i18n.enforce_available_locales = true The possible values are: false: if you want to … Read more
I believe what you are looking for is assign_attributes. It’s basically the same as update_attributes but it doesn’t save the record: class User < ActiveRecord::Base attr_accessible :name attr_accessible :name, :is_admin, :as => :admin end user = User.new user.assign_attributes({ :name => ‘Josh’, :is_admin => true }) # Raises an ActiveModel::MassAssignmentSecurity::Error user.assign_attributes({ :name => ‘Bob’}) user.name # … Read more
Here’s what I do… CoffeeScript: ready = -> …your coffeescript goes here… $(document).ready(ready) $(document).on(‘page:load’, ready) last line listens for page load which is what turbo links will trigger. Edit…adding Javascript version (per request): var ready; ready = function() { …your javascript goes here… }; $(document).ready(ready); $(document).on(‘page:load’, ready); Edit 2…For Rails 5 (Turbolinks 5) page:load becomes … Read more
Here are all the Rails 4 (ActiveRecord migration) datatypes: :binary :boolean :date :datetime :decimal :float :integer :bigint :primary_key :references :string :text :time :timestamp Source: http://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/SchemaStatements.html#method-i-add_column These are the same as with Rails 3. If you use PostgreSQL, you can also take advantage of these: :hstore :json :jsonb :array :cidr_address :ip_address :mac_address They are stored as … Read more
So I found it out by myself. It is actually a pretty simple but powerful concept. It has to do with code reuse as in the example below. Basically, the idea is to extract common and / or context specific chunks of code in order to clean up the models and avoid them getting too … Read more
Use rand(range) From Ruby Random Numbers: If you needed a random integer to simulate a roll of a six-sided die, you’d use: 1 + rand(6). A roll in craps could be simulated with 2 + rand(6) + rand(6). Finally, if you just need a random float, just call rand with no arguments. As Marc-AndrĂ© Lafortune … Read more