Tutorial by Examples

Rails.cache, provided by ActiveSupport, can be used to cache any serializable Ruby object across requests. To fetch a value from the cache for a given key, use cache.read: Rails.cache.read('city') # => nil Use cache.write to write a value to the cache: Rails.cache.write('city', 'Duckburgh'...
You can use the ActionPack page_caching gem to cache individual pages. This stores the result of one dynamic request as a static HTML file, which is served in place of the dynamic request on subsequent requests. The README contains full setup instructions. Once set up, use the caches_page class meth...
Rails >= 3 comes with HTTP caching abilities out of the box. This uses the Cache-Control and ETag headers to control how long a client or intermediary (such as a CDN) can cache a page. In a controller action, use expires_in to set the length of caching for that action: def show @user = User....
Imagine the following situation: foo :: Show a => (a -> String) -> String -> Int -> IO () foo show' string int = do putStrLn (show' string) putStrLn (show' int) Here, we want to pass in a function that converts a value into a String, apply that function to both a string p...
To generate a new mailer, enter the following command rails generate mailer PostMailer This will generate a blank template file in app/mailers/post_mailer.rb named PostMailer class PostMailer < ApplicationMailer end Two layout files will also be generated for the email view, one for the...
added in GHC 7.8. OverloadedLists, similar to OverloadedStrings, allows list literals to be desugared as follows: [] -- fromListN 0 [] [x] -- fromListN 1 (x : []) [x .. ] -- fromList (enumFrom x) This comes handy when dealing with types such as Set, Vector and Maps. ['0'...
“Fat Model, Skinny Controller” refers to how the M and C parts of MVC ideally work together. Namely, any non-response-related logic should go in the model, ideally in a nice, testable method. Meanwhile, the “skinny” controller is simply a nice interface between the view and model. In practice, this...
GCD will guarantee that your singleton only gets instantiated once, even if called from multiple threads. Insert this into any class for a singleton instance called shared. + (instancetype)shared { // Variable that will point to the singleton instance. The `static` // modifier makes it ...
To differentiate between plural and singular strings, you can define a plural in your strings.xml file and list the different quantities, as shown in the example below: <?xml version="1.0" encoding="utf-8"?> <resources> <plurals name="hello_people&quo...
Given a file file.txt with the following content: line 1 line 2 line 3 You can add a new line after first matching line with the a command. For portable use the a command must be followed immediately by an escaped newline, with the text-to-append on its own line or lines. sed ' /line 2/a\ ...
xml of the Dialog: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="mat...
String filename = "image.png"; String imagePath = getExternalFilesDir() + "/" + filename; Picasso.with(context) .load(new File(imagePath)) .into(imageView);
Add below to your build.gradle out of android tag: // Apply plug-in to app. apply plugin: 'com.google.gms.google-services' Add below helper class to your util package: /** * Created by Andy */ public class GoogleSignInHelper implements GoogleApiClient.OnConnectionFailedListener, ...
Filters are methods that are run "before", "after" or "around" a controller action. They are inherited, so if you set any in your ApplicationController they will be run for every request your application receives. Before Filter Before filters are executed before the c...
Rails provides a lot of generators, for controllers too of course. You can generate a new controller by running this command in your app folder rails generate controller NAME [action action] [options] Note: You can also use rails g alias to invoke rails generate For example, to generate a cont...
You can rescue a RecordNotFound exception with a redirect instead of showing an error page: class ApplicationController < ActionController::Base # your other stuff rescue_from ActiveRecord::RecordNotFound do |exception| redirect_to root_path, 404, alert: I18n.t("errors.record...
Configuration files for rails can be found in config/environments/. By default rails has 3 environments, development, production and test. By editing each file you are editing the configuration for that environment only. Rails also has a configuration file in config/application.rb. This is a common...
Suppose you have multiple lines in the same plot, each of a different color, and you wish to make a legend to tell what each line represents. You can do this by passing on a label to each of the lines when you call plot(), e.g., the following line will be labelled "My Line 1". ax.plot(...
Database configuration of a rails project lies in a file config/database.yml. If you create a project using rails new command and don't specify a database engine to be used then rails uses sqlite as the default database. A typical database.yml file with default configuration will look similar to fol...
Callbacks offer a way to extend the functionality of a function (or method) without changing its code. This approach is often used in modules (libraries / plugins), the code of which is not supposed to be changed. Suppose we have written the following function, calculating the sum of a given array ...

Page 366 of 1336