Ruby on Rail: Flash messages

It’s basically a messaging system that flashes messages for a few seconds on page loads.

In the controller, you usually load the flash messages like this:

flash[:success] = “Welcome to the Sample App!”

And in the views (usually a designated place in the layout view), you can display it as:

<% flash.each do |key, value| %>
<div class=”alert alert-<%= key %>”><%= value %></div>
<% end %>

Flash messages are stored in the session, and are cleared at the next request time.

You can combine redirect and flash messages as follows:

redirect_to @project, notice: "Project has been created."

Ruby On Rails: database stuff

Since you are dealing with the database layer through the Models, you usually don’t need to worry about direct access to that layer, but here are a few commands to cleanup stuff:

bundle exec rake db:reset  // Truncates the database tables

bundle exec rake db:test:prepare // To prepare the data for tests

rake db:seed # take the data from the seed file, and puts it on the database

Note: after Rails 4.1, you don’t need to run bundle exec rake db:test:prepare after a migration, they are kept automatically sync, by this line inside spec/rails_helper.rb:

ActiveRecord::Migration.maintain_test_schema!

Ruby on Rails: routes

resources :users

Basically, adding the line above to your config/routes.rb will make sure that REST style urls are available to access the User model.

If you have a line like:

get “users/new”

in your router, you can remove it now, since the resource line will take care of adding all the “add”, “create”, “update”, “delete” capabilities to the router

If you only want to make certain resources available (not all):

resources :sessions, only: [:new, :create, :destroy]

An example of one of the simplest forms for routes:
get ‘/teams/home’ => ‘teams#index’

The URL /teams/home will call the controller teams, and the action method index inside of that controller. If you want to pass extra params to this method, you can do stuff like:
get ‘/teams/search/:query’ => ‘teams#search’

To be able to reuse the route later inside rails, to, for example, create links, you can assign a name to the route as follows:

get '/teams/search/:query' => 'teams#search', :as => 'search'

And then, linking can be done via:
link_to "Search here" search_path

if you use search_url, you will get the entire URL (not just the relative path)

rake routes # will list all your available routes
You can also get that via http://localhost:3000/rails/info

Redirection

Redirection is implemented in the browser, but the server send the actual response that triggers it. In rails, this is done via: redirect_to

This method takes a URL as a parameter, but the actual URL can be dynamically created via rails, for example:
redirect_to(article_path(:id=> @article))
redirect_to(@article) # same as above… simplified

redirect_to also takes a :notice param, that acts the same way as having a flash[:notice]

Nested resources

If you see something like this:

resources :articles do
    resources :comments
  end

What it is really saying is that comments can’t exists as a standalone page, they depend on an article page to be displayed.