Kategorier



Tags

Velkommen til aarhus.rb      

aarhus.rb er en Ruby User Group for Århus og omegn.
Mailingliste aarhusrb@googlegroups.com, tilmeld dig her

Møde hos Lenio, 11. januar kl. 16-18

January 3rd, 2010 |

Program

Notes on formtastic and validation

December 10th, 2009 |

Lars Høg’s notes from the 2009-12-07 aarhus.rb meeting

Easy form building and client-side validations

Starring Formtastic, Validation _ reflection and Validatious _ on _ rails

  1. Setup

1.1. Roll a new Rails app

$ rails forms_demo
$ cd forms_demo
$ git init && git add . && git commit -m "Initial commit"

1.2. Create some scaffolds to play with …

$ ./script/generate scaffold article author:belongs_to title:string body:text section:integer publication_state:belongs_to category:belongs_to allow_comments:boolean keywords:string extract:text description:text url_title:string
$ ./script/generate scaffold author first_name:string last_name:string
$ ./script/generate scaffold category name:string
$ ./script/generate scaffold publication_state name:string

Set some validations as well in app/models/article.rb :

accepts_nested_attributes_for :author, :allow_destroy => true

validates_presence_of :title, :body, :section, :publication_state, :category, :allow_comments
validates_length_of :title, :within => 4..40

Bootstrap the database:

$ rake db:migrate
$ git add . && git commit -m "with some scaffolding"

Create a couple of categories and publication_state as well:

$ ./script/console

%w(cat1 cat2 cat3).each do |name|
  Category.create! :name => name
end
%w(Draft Reviewed Published Archived).each do |name|
  PublicationState.create! :name => name
end

$ ./script/server

2.1. Installing Formtastic

With a Rails template formtastic _ template.rb:

gem 'formtastic'
rake "gems:install"
generate "formtastic"

$ rake rails:template LOCATION=formtastic_template.rb

Add the stylesheet in the section of app/views/layouts/application.html.erb :

<%= stylesheet_link_tag 'formtastic', 'formtastic_changes' %>

Edit app/views/articles/[new|edit].html.erb :

<%= render :partial => 'form' %>

Create the form app/views/articles/_form.html.erb

<% semantic_form_for @article do |form| %>

  <% form.inputs :name => "Basic" do %>
    <%= form.input :title %>
    <%= form.input :body %>
    <%= form.input :created_at, :input_html => { :disabled => true } %>
    <%= form.input :section %>
    <%= form.input :publication_state, :as => :radio %>
    <%= form.input :category %>
    <%= form.input :allow_comments, :label => "Allow commenting on this article" %>
  <% end %>

  <% form.inputs :name => "Advanced" do %>
    <%= form.input :keywords, :required => false, :hint => "Example: ruby, rails, forms" %>
    <%= form.input :extract, :required => false %>
    <%= form.input :description, :required => false %>
    <%= form.input :url_title, :required => false %>
  <% end %>

  <% form.inputs :name => "Author", :for => :author do |author_form| %>
    <%= author_form.input :first_name  %>
    <%= author_form.input :last_name  %>
  <% end %>

  <% form.buttons do %>
    <%= form.commit_button %>
  <% end %>

<% end %>

Edit app/controllers/articles_controller.rb; add to new action after instantiating the article - note: not an actual working solution more just to have something to show:

@article.author = Author.new

2.2. Rails I18n

First we need to enable this in config/initializers/formtastic.rb and set a default locale in config/environment.rb .

Grab the latest translation from and place it under config/locales/

Create a localized file for formtastic - eg config/locales/da.forms.yml :

da:
  # use the usual format for localizing your models
  activerecord:
    models:
      article: "Artikel"
      author: "Forfatter"
      category: "Artikel kategori"
      publication_state: "Publiceringstilstand"
    attributes:
      article:
        title: "Titel"
        body: "Brødtekst"

  formtastic:
    titles:
      basic: "Artikel stamoplysninger"
      advanced: "Flere oplysninger"
      author: "Forfatter"
    labels:
      article:
        title: "Choose a title..."
        body: "Write something..."
        edit:
          title: "Edit title"
    hints:
      article:
        title: "Choose a good title for you article."
        body: "Write something inspiring here."
    actions:
      create: "Opret ny {{model}}"
      update: "Opdatér {{model}}"
      # any other actions (eg sign_up, sign_in, sign_out, ...)

And add the input section titles in the form replace:

<% form.inputs :name => "Basic" do %>

with

<% form.inputs :article_basic do %>
  1. Add some model-validation using Validation_reflection

    $  ./script/plugin install git://github.com/redinger/validation_reflection
    

You may now edit the form and remove all :required => false

With a template file validatious _ on _ rails _ template.rb with the line:

gem 'validatious-on-rails'
rake "gems:install"
generate 'validatious' # note: subject to change to: 'v2'

$ rake rails:template LOCATION=validatious_on_rails_template.rb

Add the class ‘validate’ to the form:

<% semantic_form_for @article, :html => {:class => 'validate'} do |form| %>

Edit the template and add the following JS includes:

<%= javascript_include_tag 'XMLHttpRequest' ,'v2.standalone.full.min', 'v2.config', 'v2.rails', :cache => :validatious %>
<%= javascript_for_validatious %>

Now add some validations in app/models/author.rb

has_many :articles

validates_presence_of :first_name, :last_name
validates_length_of :first_name, :last_name, :within => 2..40

Møde hos Miracle Geekhouse, mandag d. 2. november kl. 16-18

October 28th, 2009 |

Mødet holdes hos Miracle Geekhouse, Rosensgade 22, 2., 8000 Århus C

Program:

Møde hos Lenio mandag d.7 september kl. 16-18

September 7th, 2009 |

Mødet holdes hos Lenio, Jægergårdsgade 122, 1, 8000 Århus C.

Bygningen er den store gule bygning og indgangen til Lenio findes i enden af bygning længst væk fra Jægergårdsgade og så op af trappen til 1. sal.

Program

Møde hos Lenio mandag d. 3. august kl. 16-18

July 31st, 2009 |

Mødet holdes hos Lenio, Jægergårdsgade 122, 1, 8000 Århus C.

Bygningen er den store gule bygning og indgangen til Lenio findes i enden af bygning længst væk fra Jægergårdsgade og så op af trappen til 1. sal.

Program

Møde hos Lenio mandag d. 8. juni kl. 16-18

May 31st, 2009 |

Mulige emner:

Møde mandag d. 6. april 16-18

March 25th, 2009 |

Sted og program følger…

Møde hos Trifork 1. december kl. 16-18

December 1st, 2008 |

Vi mødes d. 1. december, hos Trifork, Margrethepladsen 4, DK-8000 Århus C

Agenda:

Møde hos Lenio 10. november kl. 16-18

November 10th, 2008 |

Vi mødes d. 10. november, hos Lenio A/S, Jægergårdsgade 122, Århus

Agenda:

Møde 20. oktober kl. 16-18

October 6th, 2008 |

Der mangler endnu nogen, der vil sige noget og et sted, vi kan holde det :-)

Møde hos Lenio 4. august 2008 kl. 16-18

August 4th, 2008 |

Vi mødes d. 4. august, hos Lenio A/S, Jægergårdsgade 122, Århus

Agenda:

* Rack Middleware ved Kim Dalsgaard
* Baggrundsjobs i Rails ved Jørgen Orehøj Erichsen

Møde hos Lenio 2. juni 2008 kl. 16-18

May 30th, 2008 |

Vi mødes d. 2. juni, hos Lenio A/S, Jægergårdsgade 122, Århus

Agenda:

Møde d.17 marts 2008

March 3rd, 2008 |

Vi mødes d.17. marts, hos Lenio A/S, Jægergårdsgade 122, Århus

Agenda:

Møde hos Lenio d. 18. februar 2007 kl. 16-18

February 15th, 2008 |

Agenda:

Vi mødes hos Lenio A/S, Jægergårdsgade 122, 8000 Århus C.

Alle er velkomne

Møde hos Lenio d. 7. januar 2007 kl. 16-18

January 4th, 2008 |

Agenda:

Vi mødes hos Lenio A/S, Jægergårdsgade 122, 8000 Århus C