define_method in Ruby

define_method is a (private) method of the object Class. You are calling it from an instance. define_method(:my_method) do |foo, bar| # or even |*args| # do something end or, you could even use string interpolation in the method name, such as define_method("#{attribute}=") do |unencrypted_password| if unencrypted_password.nil? self.send("#{attribute}_digest=", nil) elsif !unencrypted_password.empty? instance_variable_set("@#{attribute}", unencrypted_password) cost = ActiveModel::SecurePassword.min_cost … Continue reading define_method in Ruby

new update about has_secure_password in Rails 5.2

has_secure_password takes an attribute For many years has_secure_password only allowed a default password attribute. But now you can stash whatever you want in there. Source code Allows configurable attribute name for #has_secure_password. This still defaults to an attribute named 'password', causing no breaking change. Also includes a convenience method #<strong><span style="color:#008000;">regenerate_XXX</span></strong> where +XXX+ is the name … Continue reading new update about has_secure_password in Rails 5.2

Real-Time Rails: Implementing WebSockets in Rails 5 with Action Cable

Recent years have seen the rise of "the real-time web." Web apps we use every day rely on real-time features—the sort of features that let you see new posts magically appearing at the top of your feeds without having to lift a finger. While we may take those features for granted, they represent a significant … Continue reading Real-Time Rails: Implementing WebSockets in Rails 5 with Action Cable

helper_method in Rails

The method helper_method is to explicitly share some methods defined in the controller to make them available for the view. This is used for any method that you need to access from both controllers and helpers/views (standard helper methods are not available in controllers). e.g. common use case: #application_controller.rb def current_user @current_user ||= User.find_by_id!(session[:user_id]) end … Continue reading helper_method in Rails

Configure Rails on Windows 7

1. Go to https://rubyinstaller.org/downloads/ and download the recommended (not necessarily the latest version of the Ruby with Devkit to avoid potential gem compatibility issue (for example, sqlite3 on Windows platform) Which version to download? If you don’t know what version to install and you’re getting started with Ruby, we recommend you use Ruby+Devkit 2.4.X as … Continue reading Configure Rails on Windows 7