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.