Ruby Rail First Step: Difference between revisions

From Objectif Client Inc
Jump to navigation Jump to search
No edit summary
No edit summary
Line 1: Line 1:
* Your first application with MySQL
* Your first application with MySQL


==Create the Application==
<pre>rails new firstapp -d mysql</pre>
<pre>rails new firstapp -d mysql</pre>


Line 22: Line 23:
<pre>root :to => "home#index"</pre>
<pre>root :to => "home#index"</pre>


# Generate a ressource
==Start to Work with Resources==
 
===Generate a resource (Post)===
<pre>rails generate scaffold Post name:string title:string content:text</pre>
<pre>rails generate scaffold Post name:string title:string content:text</pre>


[http://www.railsdebutant.org/fr_guides/generators.html Creating and Customizing Rails Generators & Templates]
[http://www.railsdebutant.org/fr_guides/generators.html Creating and Customizing Rails Generators & Templates]


* Create Tables
===Create Tables (posts)===
<pre>rake db:migrate</pre>
<pre>rake db:migrate</pre>


To create in production
* To create in production
<pre>rake db:migrate RAILS_ENV=production.</pre>
<pre>rake db:migrate RAILS_ENV=production.</pre>


===Add a link into the home page===
* Add Post Page in the index file app/view/home/index.html.erb
* Add Post Page in the index file app/view/home/index.html.erb
<pre><%= link_to "My Blog", posts_path %> </pre>
<pre><%= link_to "My Blog", posts_path %> </pre>
===Add validation===
*Add validation into app/modles/post.rb
<pre>class Post < ActiveRecord::Base
  validates :name,  :presence => true
  validates :title, :presence => true,
                    :length => { :minimum => 5 }
end

Revision as of 23:13, 14 November 2014

  • Your first application with MySQL

Create the Application

rails new firstapp -d mysql
  1. Move into the application directory
cd firstapp
  1. If you setup MySQL or Postgres with a username/password, modify the
  2. config/database.yml file to contain the username/password that you specified
  1. Create the database
rake db:create
  1. Start the server
rails server
  1. Visite the web site

http://localhost:3000

  1. change the default page

Add the following into config/routes.rb

root :to => "home#index"

Start to Work with Resources

Generate a resource (Post)

rails generate scaffold Post name:string title:string content:text

Creating and Customizing Rails Generators & Templates

Create Tables (posts)

rake db:migrate
  • To create in production
rake db:migrate RAILS_ENV=production.

Add a link into the home page

  • Add Post Page in the index file app/view/home/index.html.erb
<%= link_to "My Blog", posts_path %> 

Add validation

  • Add validation into app/modles/post.rb
class Post < ActiveRecord::Base
  validates :name,  :presence => true
  validates :title, :presence => true,
                    :length => { :minimum => 5 }
end