background preloader

Rails Routing from the Outside In

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. 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 2.3 Path and URL Helpers 2.5 Singular Resources

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.

Michael Hartl’s 15 Hours of Rails 3 Screencasts By Peter Cooper / October 13, 2010 Have you seen Michael Hartl's RailsTutorial.org? It's a free online "book" that walks you through from start to finish with building either a Rails 2.3 or Rails 3.0 app (though a $39 PDF rendering is also available). Michael's project is the latest in a line of self publishing efforts in the Ruby and Rails communities and it's gone down a storm on Hacker News. At $85, they might seem steep to some readers, but if you basically want to be able to look "over the shoulder" of an experienced Rails developer and see how a Rails development environment is set up and how multiple apps are built, there's nothing that can beat this. So if you want to learn Rails 3.0 in a practical manner from the ground up, check out Michael's screencasts.

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

Ruby on Rails guides 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.

Top 12 Ruby on Rails Tutorials A former student asked me a few days ago how I learned Ruby on Rails. The answer was that I simply read alot of great tutorials. So in the spirit of sharing, here are the 12 tutorials that I found most useful: Rolling with Ruby on Rails – Curtis Hibbs of ONLamp.com offers his first excellent introduction to Ruby on Rails. This is the article that got me really excited about RoR.2. Hey, Ruby on Rails Fans! UPDATE, JUNE 2009: Want more up-to-date tutorials on Ruby programming? Happy Rails developing and if you have any other tutorials that you’d like to share, please leave them in the comments! Tagged as: ruby on rails, tutorial

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

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▲

Related: