Saturday, July 6, 2013

Composer and rails 4

I'm trying to catch up with rails 4, since the official release was published few weeks ago. I've installed Ruby 2.0 as well to keep everything up to date. Now all my books of rails and Ruby are obsolete.

So what I want to try today is to use the Composer gem which is a collection of useful tools for creating a new app. This include an interactive menu where you are offer to include different configurations such as, visual template, security, type of server, test framework, etc.

I'm following this documentation for the setup:

https://github.com/RailsApps/rails-composer/blob/master/README.textile


$ mkdir vega
$ cd vega
$ rvm use ruby-2.0.0@myapp --ruby-version --create
$ gem install rails
$ rails new . -m https://raw.github.com/RailsApps/rails-composer/master/composer.rb -T

The reason for -T is because this way you can add the -T flags to skip Test::Unit if you are using RSpec for testing.



Here is the menu you will see, right after you create the new rails app:



Initialized empty Git repository in /Users/Snake/apps/vega/.git/
         run    git add -A from "."
         run    git commit -qm "rails_apps_composer: initial commit" from "."
      recipe  Running railsapps recipe...
    question  Install an example application for Rails 4.0?
          1)  Build a RailsApps starter application
          2)  Build a contributed application
          3)  I want to build my own application
   railsapps  Enter your selection: 3
      recipe  Running setup recipe...
       setup  Your operating system is darwin9.8.0.
       setup  You are using Ruby version 2.0.0.
       setup  You are using Rails version 4.0.0.
    question  Web server for development?
          1)  WEBrick (default)
          2)  Thin
          3)  Unicorn
          4)  Puma
       setup  Enter your selection: 1
    question  Web server for production?
          1)  Same as development
          2)  Thin
          3)  Unicorn
          4)  Puma
       setup  Enter your selection: 1
    question  Database used in development?
          1)  SQLite
          2)  PostgreSQL
          3)  MySQL
          4)  MongoDB
       setup  Enter your selection: 1
    question  Template engine?
          1)  ERB
          2)  Haml
          3)  Slim (experimental)
       setup  Enter your selection: 1
    question  Unit testing?
          1)  Test::Unit
          2)  RSpec
          3)  MiniTest
       setup  Enter your selection: 2
    question  Integration testing?
          1)  None
          2)  RSpec with Capybara
          3)  Cucumber with Capybara
          4)  Turnip with Capybara
          5)  MiniTest with Capybara
       setup  Enter your selection: 2
    question  Continuous testing?
          1)  None
          2)  Guard
       setup  Enter your selection: 1
    question  Fixture replacement?
          1)  None
          2)  Factory Girl
          3)  Machinist
          4)  Fabrication
       setup  Enter your selection: 2
    question  Front-end framework?
          1)  None
          2)  Twitter Bootstrap
          3)  Zurb Foundation
          4)  Skeleton
          5)  Just normalize CSS for consistent styling
       setup  Enter your selection: 2
    question  Twitter Bootstrap version?
          1)  Twitter Bootstrap (Less)
          2)  Twitter Bootstrap (Sass)
       setup  Enter your selection: 2
    question  Add support for sending email?
          1)  None
          2)  Gmail
          3)  SMTP
          4)  SendGrid
          5)  Mandrill
       setup  Enter your selection: 2
    question  Authentication?
          1)  None
          2)  Devise
          3)  OmniAuth
       setup  Enter your selection: 2
    question  Devise modules?
          1)  Devise with default modules
          2)  Devise with Confirmable module
          3)  Devise with Confirmable and Invitable modules
       setup  Enter your selection: 2
    question  Authorization?
          1)  None
          2)  CanCan with Rolify
       setup  Enter your selection: 2
    question  Use a form builder gem?
          1)  None
          2)  SimpleForm
       setup  Enter your selection: 2
    question  Install a starter app?
          1)  None
          2)  Home Page
          3)  Home Page, User Accounts
          4)  Home Page, User Accounts, Admin Dashboard
       setup  Enter your selection: 4
here are some messages from the installation. I will have to check them carefully:

First. Bootstrap requires some actions:
========================================================================
Be sure to have a copy of the Bootstrap stylesheet available on your application, you can get it on http://twitter.github.com/bootstrap. Inside your views, use the 'simple_form_for' with one of the Bootstrap form classes, '.form-horizontal', '.form-inline', '.form-search' or '.form-vertical', as the following: = simple_form_for(@user, html: {class: 'form-horizontal' }) do |form|
======================================================================== Some setup you must do manually if you haven't yet: 1. Setup default url options for your specific environment. Here is an example of development environment: config.action_mailer.default_url_options = { :host => 'localhost:3000' } This is a required Rails configuration. In production it must be the actual host of your application 2. Ensure you have defined root_url to *something* in your config/routes.rb. For example: root :to => "home#index" 3. Ensure you have flash messages in app/views/layouts/application.html.erb. For example: <p class="notice"><%= notice %></p> <p class="alert"><%= alert %></p> 4. If you are deploying Rails 3.1 on Heroku, you may want to set: config.assets.initialize_on_precompile = false On config/application.rb forcing your application to not access the DB or load models when precompiling your assets. ========================================================================

Sunday, June 9, 2013

Simple form

Add the gem to your GemFile
gem 'simple_form'

run bundle for downloading the Gem
bundle install


Deploy the gem:
rails g simple_form:install

This creates some files under your project

      create  config/initializers/simple_form.rb       exist  config/locales      create  config/locales/simple_form.en.yml      create  lib/templates/erb/scaffold/_form.html.erb

Following the previous sample of books, these are the only lines you need for your _from.html.erb under app/views/books

<%= simple_form_for(@book) do |f| %>
    <%= f.input :name %><br />
    <%= f.association :status %><br />
    <%= f.button :submit %><br />
<% end %>

Notices that we use simple_form_for instead form_for and just one line for each component. simple, hun?


Check out the video from railcast

Drop down select list

One of the first thing I encounter was to find a way how to associate tables. The simples association could be for example this:
You have several books represented by the Book object and stored in the books table in the DB. This books have a status that could be Available, Borrowed or Missing. Now, in order to represent that in Rails you need to create those two classes and associate them so in the View you can select from a drop down list the correct status.

Let's start creating a rails project, open your console and type:

rails new library
cd library

create the objects

rails g scaffold Book name:string status_id:integer


rails g scaffold Status name:string

create the tables for each object
rake db:migrate

In the models you have to add the association for each class.

For book:
class Book < ActiveRecord::Base
  attr_accessible :name, :status_id
  belongs_to :status
end

For status:
class Status < ActiveRecord::Base
  attr_accessible :name
  has_one :book
end

To learn more about association here is the documentation

Now if run the server with:
rails s

and if you go to the following address you will be able to create the three statuses required
http://localhost:3000/statuses

and then go to the url:
http://localhost:3000/books/new

you will see that instead of having status, you have the index, but this is not what we want.


for having the name of the status you have to change something in the view of the book


From your editor open the file app/views/books/_form.html.erb

and this is the code we have to change:

  <div class="field">
    <%= f.label :status_id %><br />
    <%= f.number_field :status_id %>
  </div>


by this one, which basically creates the drop down list for you:

  <div class="field">
    <%= f.label :status_id %><br />
    <%= collection_select :book, :status_id, Status.all, :id,:name,:promp => true  %>
  </div>

The signature of collection_select is this:

collection_select(object, method, collection, value_method, text_method, options = {}, html_options = {})

Basically the two first parameters of collection_select indicate where do you want to save the selection. In this case: Book.status_id
Status.all retrieves all the values from the status table and loads them into the drop down list.
The following two parameters indicate how to create the drop down list, the first is the value, which is the index or primary key of the status, and the second is the text which is the name of the status

For more information on how to use collection select, check out the documentation

Start off

Hi and welcome to this blog!

During the last year I have been following Ryan Bates and his posts at http://railscasts.com/ so I decided I wanted to do an web application. I tried to start developing directly but it is not as easy as it is for Ryan. So my plan B is split my app and go by pieces.

Well we are again, a new blog, a new technology. This one will be for Ruby on rails 3.2. Here I will post what I have encountered difficult or at least what took me some time to figure out during my web application development. Basically this is for sharing with others but specially for my own tracking. I hope you'll find it useful.