Categories
Rails sass

Ruby On Rails: layouts and partials

layouts

The default layout (applied to all controllers) is: app/views/layouts/application.html.erb You can overwrite it by:

  • creating a erb file with the name of the controller you want to make a custom layout for in the layouts directory (for example: app/views/layouts/mycontroller.html.erb)
  • explicitly overwrite the layout to be used inside the controller:
    class ExampleController < ApplicationController
      layout 'my_layout' # Will use a layout in app/views/layouts/my_layout.html.erb
    end
  • explicitly specify a layout in an action inside a controller (just for that action): render :layout => ‘my_layout’
  • explicitly state you don’t want a layout: render :layout => false

partials

app/views/layouts/_footer.html.erb (take note of the underscore before the partial name)

It is called from the code as:
<%= render ‘layouts/footer’ %>

You can pass arguments to partials as follows:

<%= render 'header', :title => 'My Blog' %>

partials have a shortcut to render objects, as follows:
<%= render @article %>
It is the equivalent to:
<%= render ‘articles/article’, :article => @article %>

It basically renders the _article partial under articles, with the object assigned to the variable @article.

You could even render a collection of articles, via:
<%= render @articles %>

Note: if you overwrite views/layouts/application, you may want to include the following tag in it:

<%= csrf_meta_tags %>

This is the tag that automatically generates a security token when you post forms, use it to avoid cross domain attacks.

stylesheet_link_tag

stylesheet_link_tag is for including CSS stylesheets from the app/assets/stylesheets directory. The links have a cache buster that is based on the digest content of the files. Example of the output in the HTML of this:

<link rel="stylesheet" href="/assets/projects-[digest].css?x52773"
media="all" data-turbolinks-track="true" />
<link rel="stylesheet" href="/assets/application-[digest].css?x52773"
media="all" data-turbolinks-track="true" />

Sprockets is the name of the gem that takes care of the assets pipeline.

The other css link assets pipeline will take care of for you look on the following directories: will app/assets, lib/assets, and vendor/assets, in that order, and generate a concatenated CSS file with the contents of the css files there. In development mode you get a non-concatenated version of it.

Inside app/assets, if you have a .scss extension, your file will get sass pre-processed before it gets generated. There is usually a [name of your app].scss file there waiting to be filled.

javascript_include_tag

javascript_include_tag, when in the application.html.erb page, loads the javascript from the app/assets/javascripts/application.js directory.

This ends up loading jquery, turbolinks, and any files contained inside app/assets/javascripts, and concatenate them into a javascript output file in: application.js

In development mode, you get all these files separated from each other.

Any files with the .coffee extension will be treated as coffescript files, and precompiled into javascript along the way.

Categories
Rails

Ruby on Rails: creating an app from the ground up

$ rails new sample_app -T
The -T is to turn off the default test unit environment, so we can use RSpec instead (OPTIONAL)

Then you should include the RSpec gems you will need for testing (in your Gemfile) if you turn off the default test env:
group :development do
gem ‘rspec-rails’, ‘2.0.1’
end

group :test do
gem ‘rspec’, ‘2.0.1’
gem ‘webrat’, ‘0.7.1’
end

Make any other changes you need to do inside the Gemfile file (additional gems, remove gems you don’t need, etc). Notice you don’t need to specify the gem version numbers, but if you don’t, the latest will be loaded at bundle install time, so better specify the version numbers you trust as working.

If you are planning to use Heroku, make sure you specify your sqlite gem will only be used in the dev env:

group :development do

gem ‘sqlite3’, ‘1.3.5’

end

Then you run (to finish the gems installation):
$ bundle install
$ rails generate rspec:install

If you get any errors about gems not on your system “Could not find gem x…”, run the following command:

gem install x

Then we initialize git:

$ git init
$ git add .
$ git commit -m “Initial commit”

At developing time, if you are doing major modifications to the code, you may want to branch:
$ git checkout -b new-branch-name-here

You are set! To run your newest project in your dev env:

rails server

Categories
Rails

Ruby on Rails: scaffolding

rails generate scaffold User name:string email:string

Creates all the necessary files to be able to add / edit / delete a user object via a web interface

Notice that Model uses singular “User”, controllers usually use “Users”

By supplying the name and email arguments, we also create the db tables needed for the user.

If you already have the models in place, and you don’t want to generate another migration, you can use the following option to avoid creating one:

--skip-migration
Categories
Mobile

Gotcha: Including Jquery mobile in the page will hide all page elements

If you are using phoneGap inside dreamweaver, and setup a mobile site, but don’t use Jquery library to make the UI, don’t forget to remove the CSS and JS references to the mobile library that dreamweaver puts there by default, because if you don’t, your document will not show any HTML at all, confusing the heck out of you for awhile…

Categories
Mobile

mobile: xcode build fails but no build errors reported

If your bundle identifier has an underscore “_” due to the app name having a blank space in the name, this will be the cause.

Just get rid of the underscore, run clean, and rebuild again.

Categories
JavaScript

Javascript: check if an object exist, append to it or create it if it doesn’t

Amazingly, the syntaxis is really simple:

var canvasExample = canvasExample || {};

God I love javascript!

Note: it doesn’t work on variables