Sunday, March 6, 2011

command in RAILS


1.Create application
C:\InstantRails-2.0-win_2\rails_apps\pravin>rails diligence
2.create database by command  and configure in config/databse.yml  or directly do both by command line database creation and configuration simultaneously.
C:\InstantRails-2.0-win_2\rails_apps\pravin>mysqladmin -u root create diligence_development
3.Database configuration
C:\InstantRails-2.0-win_2\rails_apps\pravin>development:
          Adapter: mysql
          Databse: diligence_development
          Username: root
          Password:
          Host: localhost
4.Description:
AdapterC:\InstantRails-2.0-win_2\rails_apps\pravin>Adapter tell rails what kind of database using.
DatabaseC:\InstantRails-2.0-win_2\rails_apps\pravin>gives the name of database.
Username and passwordC:\InstantRails-2.0-win_2\rails_apps\pravin>let our application to log in database.
HostC:\InstantRails-2.0-win_2\rails_apps\pravin>tells rails the computer name where application is running.
5. OR
C:\InstantRails-2.0-win_2\rails_apps\pravin>cd diligence
C:\InstantRails-2.0-win_2\rails_apps\pravin>rails –database=mysql diligence
6.Test your database configuration
C:\InstantRails-2.0-win_2\rails_apps\pravin>rake db:create RAILS_ENV=’development’
C:\InstantRails-2.0-win_2\rails_apps\pravin>rake db:migrate
7.Database Migration
C:\InstantRails-2.0-win_2\rails_apps\pravin>database migration represent a change we  want to make in database, expressed in a source file in database independent terms. This changes can update both in database SCHEMA and database TABLES.
8.Create product tables via scaffold
C:\InstantRails-2.0-win_2\rails_apps\pravin>ruby script/generate scaffold product title:string description:text image_url:string
9.Again we have need to do is to get rails to apply this migration to our development database.
C:\InstantRails-2.0-win_2\rails_apps\pravin>rake db:migrate
OK, all the groundwork has been done. We set up our diligence  application as a Rails project. We created the development database and configured our application to be able to connect to it. We created a product controller and a product model and used a migration to create the corresponding products table. And a number of views have been created for us. It’s time to see all this in action.

10.ROLL BACK YOUR APPLICATION
C:\InstantRails-2.0-win_2\rails_apps\pravin>rake db:migrate VERSION=0
(our schema will roll bake in previous time)

11.Now start webrick web based server

C:\InstantRails-2.0-win_2\rails_apps\pravin>ruby script/server

12.Adding missing colons

C:\InstantRails-2.0-win_2\rails_apps\pravin>ruby script/generate migration add_price_to_product price:decimal

13.Validating Input Field in Index Page.

Open app/models/product.rb

validates__presence_of :title, :description, :image_url
validates_mumericallity_of :price
validates_uniquness_off :title
validate :price_must_be_at_least_a_cent
validates_formate_of :image_url,
                                   :width=>%r{\.(jpg/png/gif)$}i,
                                   :message=>’must be url in PNG/JPG/GIF formate’

protecded:
def price_must_be_at_least_a_cent
         
          errors.add(:price ‘should at least .01 or greater’) if price.nil? || price<.01

end

14.Convert rupees into CURRENCY($ {DOLLER})
 
è<span class="price" ><%= number_to_currency(product.price) %></span>

15.Create controller along width view

C:\InstantRails-2.0-win_2\rails_apps\pravin>ruby script/generate controller store index

16. Link tag in rails

è<%= link_to “Hit me”, :action=>’edit’, :method=>:post %>

17.Button in rails

è<%= button_to “submit” %>

18.Image tag in rails

è<%= image_tag(product.image_url) %>

19. Add stylesheet in layout Page

è<%= stylesheet_link_tag “pravin”, :media=>”all” %>

20.Remove HTML tag

è<%=h truncate(product.description.gsub(/<.*?>/,''),
:length => 80) %>

21.Display Time

è<%= Time.now %>

22.Create Migration Containing our session

C:\InstantRails-2.0-win_2\rails_apps\pravin>rake db:session:create

23. Create new cart if not present

èsession[:cart] ||=Cart.new

24.NOTE: ruby generator only use for create database-backed models

25.How to get id, requested by button

èproduct=Product.find(parms[:id])
èparams object is use to hold the parameter pass by browser.

26. Delete old session

C:\InstantRails-2.0-win_2\rails_apps\pravin>rake db:sessions:clear

27. Command for list all rails task
C:\InstantRails-2.0-win_2\rails_apps\pravin>rails –tasks

28.To lock your application on current version.

C:\InstantRails-2.0-win_2\rails_apps\pravin>rake rails:freeze:gems
29. To lock your application on current version.

C:\InstantRails-2.0-win_2\rails_apps\pravin>rake rails:unfreeze

30.Freeze to recent version

C:\InstantRails-2.0-win_2\rails_apps\pravin>rake rails:freeze:edge

Some important directories


App/Test->Most of our work takes placed in app/test directory.
DocàThe doc directory is used for application documentation. It is produced using RDoc. If you run rake doc:app, you’ll end up with HTML documentation in the directory doc/app.

LibàThe lib directory holds application code that doesn’t fit neatly into a model,view, or controller. For example, you may have written a library that creates PDF receipts that your store’s customers can download.
1 These receipts are sent directly from the controller to the browser (using the send_data method).
The code that creates these PDF receipts will sit naturally in the lib directory.
The lib directory is also a good place to put code that’s shared among models,views, or controllers.

Rake TasksàRails provides a Rake task to tell you the latest migration that has been performed.

LogàAs Rails runs, it produces a bunch of useful logging information. This is stored (by default) in the log directory. Here you’ll find three main log files, called development.log, test.log, and production.log. The logs contain more than just simple trace lines; they also contain timing statistics, cache information, and expansions of the database statements executed.

PublicàThe public directory is the external face of your application. The web server takes this directory as the base of the application. Much of the deployment configuration takes place here.
ScriptàThe script directory holds programs that are useful for developers. Run most of these scripts with no arguments to get usage information. About Displays the version numbers of Ruby and the Rails components being used by your application, along with other configuration information. Dbconsole Allows you to directly interact with your database via the command line.

Console->Allows you to use irb to interact with your Rails application methods. irb

Destroy->Removes auto generated files created by generate.

Generate->A code generator. Out of the box, it will create controllers, mailers, models, scaffolds, and web services. You can also download additional generator modules from the Rails website.2 Run generate with no arguments for usage information on a particular generator, for example: ruby scriptgenerate migration.

Plugin->Helps you install and administer plug-ins—pieces of functionality that extend the capabilities of Rails.

Runner->Executes a method in your application outside the context of the Web. You could use this to invoke cache expiry methods from a cron job or handle incoming e-mail.

Server->Runs your Rails application in a self-contained web server, using Mongrel (if it is available on your box) or WEBrick. We’ve been using this in our Depot application during development.

Benchmarker->Generates performance numbers on one or more methods in your application.

Profiler->Creates a runtime-profile summary of a chunk of code from your application.

Request->Creates a runtime-profile summary of a URI request processed by your application.

TempàIt probably isn’t a surprise that Rails keeps its temporary files tucked up in the tmp directory. You’ll find subdirectories for cache contents, sessions, and sockets in here.

VenderàIs this directory where third party code live.