background preloader

Start

Facebook Twitter

Rails 4.0

Ruby on Rails « Steve Sohcot's Technology Lessons Learned. Form Helpers. This guide is not intended to be a complete documentation of available form helpers and their arguments. Please visit the Rails API documentation for a complete reference. 1 Dealing with Basic Forms The most basic form helper is form_tag. When called without arguments like this, it creates a <form> tag which, when submitted, will POST to the current page. You'll notice that the HTML contains input element with type hidden. 1.1 A Generic Search Form One of the most basic forms you see on the web is a search form.

A form element with "GET" method,a label for the input,a text input element, anda submit element. To create this form you will use form_tag, label_tag, text_field_tag, and submit_tag, respectively. This will generate the following HTML: For every form input, an ID attribute is generated from its name ("q" in above example). Besides text_field_tag and submit_tag, there is a similar helper for every form control in HTML. Always use "GET" as the method for search forms. 1.3.1 Checkboxes. 4.5. Populating the Database with seeds.rb. With the file db/seeds.rb, the Rails gods have given us a way of feeding default values easily and quickly to a fresh installation.

This is a normal Ruby program within the Rails environment. You have full access to all classes and methods of your application. So you do not need to enter everything manually with rails console in order to make the records created in the section called “create” available in a new Rails application, but you can simply use the following file db/seeds.rb: Country.create(name: 'Germany', population: 81831000) Country.create(name: 'France', population: 65447374) Country.create(name: 'Belgium', population: 10839905) Country.create(name: 'Netherlands', population: 16680000) You then populate it with data via rake db:seed. I use the file db/seeds.rb at this point because it offers a simple mechanism for filling an empty database with default values.

Generating seeds.rb From Existing Data We create our own little rake task for that. Rails Routing from the Outside In. 1 The Purpose of the Rails Router The Rails router recognizes URLs and dispatches them to a controller's action. It can also generate paths and URLs, avoiding the need to hardcode strings in your views. 1.1 Connecting URLs to Code When your Rails application receives an incoming request for: it asks the router to match it to a controller action. If the first matching route is: 1.2 Generating Paths and URLs from Code You can also generate paths and URLs. And your application contains this code in the controller: and this in the corresponding view: then the router will generate the path /patients/17. 2 Resource Routing: the Rails Default Resource routing allows you to quickly declare all of the common routes for a given resourceful controller. 2.1 Resources on the Web Browsers request pages from Rails by making a request for a URL using a specific HTTP method, such as GET, POST, PATCH, PUT and DELETE.

It asks the router to map it to a controller action. 2.2 CRUD, Verbs, and Actions 3.12 Redirection. Les meilleurs cours et tutoriels Ruby et Ruby on Rails. Créer une interface graphique avec Ruby/GTK. Pour le moment, nous n'avons vu que la fenêtre, qui est un conteneur ne pouvant contenir qu'un seul widget. Il s'agissait du conteneur Gtk::Bin, dont Gtk::Window hérite. Mais ceci n'est pas suffisant pour faire une interface graphique, nous allons donc voir les autres conteneurs disponibles. Les boîtes sont un type de conteneur très simple, semblables à n'importe quel carton dans lequel on jette des revues : elles s'empilent les unes au dessus des autres, dans l'ordre dans lequel on les a jetées. Il existe des boîtes verticales (Gtk::VBox) et des boîtes horizontales (Gtk::HBox), qui héritent toutes les deux de l'objet générique Gtk::Box. Voyons quelques exemples de boîtes window = Gtk::Window.new window.set_title('VBox homogene') window.signal_connect('destroy') { Gtk.main_quit } vbox = Gtk::VBox.new(true, 6) vbox.pack_start(Gtk::Button.new('Un')) vbox.pack_start(Gtk::Button.new('Deux')) vbox.pack_start(Gtk::Button.new('Trois')) window.add(vbox) window.show_all.

Commencer avec Ruby on Rails. Ce guide s'inspire du site « Rails Guides », de mon expérience personnelle de Ruby on Rails et dans tout un tas d'autres ressources (screencasts, sites communautaires, etc...). Ce guide est également l'occasion de constituer une application de A à Z, d'en voir tous les tenants et les aboutissants (conception, codage, déploiement, etc...). Je vous souhaite d'ores et déjà une très bonne lecture ! Dans ce guide, je pars du principe que le lecteur possède une connaissance minimale du langage Ruby. Si certaines portions de code Ruby nécessitent des précisions, j'expliquerai le plus clairement possible ces points de détail. Pour commencer avec Ruby on Rails, il est nécessaire d'avoir au moins trois choses installées : Ruby (version 1.8.7 dans le cas de ce guide) Rubygems (version 1.3.2 dans le cas de ce guide) SQLite, MySQL, ou PostgreSQL Rails est un framework de développement web écrit en langage Ruby.

La philosophie de Rails se base sur trois principes : III-A. III-B. IV-A. IV-B. IV-C. Le kit du bon développeur Rails. Les tests sont une partie très importante de votre application Rails et il ne faut pas les négliger. Il existe différentes gems vous permettant d'effectuer des tests unitaires, des tests d'intégration ou encore des tests de performance. La plus répandue d'entre elles est sans doute RSpec (vous pouvez obtenir des informations sur la page Github de RSpec) mais ce n'est pas sur cette dernière que je vais m'attarder, il y a déjà un certain nombre de documentations très bien faites.

Capybara▲ Capybara est une gem permettant de tester votre site ou votre application tel que le ferait un utilisateur en navigant. Vous pouvez donc, au travers de vos tests, établir des scénarios de navigation afin de tester différentes parties de votre site ou application. If "signs user in" do within("#login") do fill_in 'Login', :with => 'user@example.com' fill_in 'Password', :with => 'password' end click_link 'Sign in'end Il est également possible de tester des éléments JavaScript avec Capybara. MiniTest▲ Spork▲