Auto complete in RoR (Ruby on Rails)

Dive to step  5 if you have already running application.

1) Create rails application

rails app_auto_complete

2) Configure your database and set any table say ‘products’.

Assumes that you have products table and name, detail fields are there in table.

3) For quick go scaffold application with

ruby script/generate scaffold Product name:string detail:text

4) Now you have running application , to start application start application

ruby script/server

5) Now install rails plugin from git repo  git://github.com/rails/auto_complete.git
if you have git install please start and get with

git clone git://github.com/rails/auto_complete.git
Put it in ~/vendor/plugins

else download manually  from repo.

6) Ok, now we have plugin for auto complete , check if you have included default javascripts in layout
if not please add in layout.

<%= javascript_include_tag :defaults %>

7) Now open file where you want to put auto complete functionality.Say if in ~app/views/product/new.rhtml

replace line <%= f.text_field :name %>
with <%= text_field_with_auto_complete ‘product’, ‘name’,{}, :skip_style => false  %>

8) Now open products controller and put this line after

class ProductsController < ApplicationController

skip_before_filter :verify_authenticity_token, :o nly => [:auto_complete_for_product_name]

9) Now create method for auto complete

def auto_complete_for_message_to()
@products = Product.find(:all , :conditions = >["name like (?)","%"+params[:product][:name].to_s+”%”])
render :partial => ‘product’
end

10) Step for showing auto compete result.
Create partial file in product folder and name it “_product.rhtml”

and put this code

<ul>
<% for product in @products do %>
<li>
<div><%=h product.name %></div>
</li>
<% end %>
</ul>

11) Finally restart you application and go to url

http://host:port/products/new (For local development)

Have fun !!!!!

For any query please post …..

Deploy Rails (Ruby on Rails)application from Windows with Heroku and Git

Deploy Rails (Ruby on Rails )application from Windows with Heroku and Git.

1) First get Git and install it.

2) If Git has not set PATH , press window+break and in advance tab set path “C:\Program Files\Git\bin”

3) Restart you system

4) Now install gem Heroku

gem install heroku

5) Make application with rails “appplication”

6) Initialize git repo.

git init

7) Now, Create your application on Heroku

heroku create

This will ask you for heroku credentil email and password once you have given correct will save in heroku
credentil file. /heroku/credentil

If you have already created application on heroku please skip this step.

8) Now it’s time to push you application to Heroku

git push heroku master

9) Now Database on Heroku

heroku rake db:migrate

Hey , your creation is on ^

In case if you need to set your rsa key just type ssh-keygen -t rsa -C “email_address” it will generate rsa key for heroku.You can find in current user ~/.ssh folder.

Have fun !!!!!

LMNBuilder.com – Your words. Your letters. Our tool.

Rails framework structure …..

Ruby on Rails is separated into various packages, namely ActiveRecord (an object-relational mapping system for database access), ActiveResource (provides web services), ActionPack, ActiveSupport and ActionMailer. Prior to version 2.0, Rails also included the Action Web Service package which is now replaced by Active Resource. Apart from standard packages, developers can make plugins to extend existing packages.Source[Wiki]

What is Rails ?

  • An extremely productive web-application framework.
  • Written in Ruby by David Heinemeier Hansson.
  • You could develop a web application at least ten times faster with Rails than you could with a typical Java framework.
  • An open source Ruby framework for developing database-backed web applications.
  • Your code and database schema are the configuration!
  • No compilation phase required.

There are a few other reasons behind Rails’ success:

Full Stack Framework

  • Includes everything needed to create a database-driven web application using the Model-View-Controller (MVC) pattern.
  • Being a full-stack framework means that all layers are built to work seamlessly together Less Code.
  • Requires fewer total lines of code than other frameworks spend setting up their XML configuration files.

Convention over Configuration

  • Rails shuns configuration files in favor of conventions, reflection and dynamic run-time extensions. Your application code and your running database already contain everything that Rails needs to know!

Don’t repeat yourself (DRY)

  • DRY is a slogan you will hear frequently associated with Ruby on Rails which means you need to code behavior only once and you never have to write similar code in two different places. This is important because you are less likely to make mistakes by modifying your code at one place only.

Rails Major Strengths:

Rails is packed with features that make you more productive, with many of the following features building on one other.

Metaprogramming : Other frameworks use extensive code generation from scratch. Metaprogramming techniques use programs to write programs. Ruby is one of the best languages for metaprogramming, and Rails uses this capability well. Rails also uses code generation but relies much more on metaprogramming for the heavy lifting.

Active Record : Rails introduces the Active Record framework, which saves objects to the database. The Rails version of Active Record discovers the columns in a database schema and automatically attaches them to your domain objects using metaprogramming.

Convention over configuration: Most web development frameworks for .NET or Java force you to write pages of configuration code. If you follow suggested naming conventions, Rails doesn’t need much configuration.

Scaffolding: You often create temporary code in the early stages of development to help get an application up quickly and see how major components work together. Rails automatically creates much of the scaffolding you’ll need.

Ajax at the core: Ajax is the technology that has become a standard to provide interactivity to websites without becoming intrusive. Ruby on Rails has a great support for Ajax technology and it is part of the core libraries. So when you install RoR, Ajax support is also made available to you.

Built-in testing: Rails creates simple automated tests you can then extend. Rails also provides supporting code called harnesses and fixtures that make test cases easier to write and run. Ruby can then execute all your automated tests with the rake utility.

Three environments: Rails gives you three default environments: development, testing, and production. Each behaves slightly differently, making your entire software development cycle easier. For example, Rails creates a fresh copy of the Test database for each test run.

What is Tagging?

Tags are really nothing more than keywords used to describe a piece of data — be it a web page, a digital photo, or another type of digital document. Of course, organizing digital images by keywords and categories has been around for a long time, it just wasn’t called “tagging” until fairly recently.

Why RoR?

Ruby on Rails has been considered to be a proven architecture and tool which could be used for the fastest growing and most efficient business solutions. It is an OOP or object oriented programming language which makes programming a lot easier, not to mention more interesting. Rails has been able to revolutionize the world of web development with its functionality and practicality. It has provided programmers with the tools to get rid of the long coding techniques as well as to enhance ruby programming.

Ruby on Rails is a free, open source web framework that uses the Ruby programming language in the development of database-backed web applications through the Model-View-Control model optimized for continuous productivity. Rails gives an exclusive Ruby development environment from the controller’s request and response to the domain model which wraps the database. In order to go live, you would just have to add a web server and a database.

A lot of web developers prefer to use Ruby on Rails because they want to make the development process a lot faster and more accurate, as Rails is truly an efficient tool. It provides easy and quick development that would still yield quality results. With the Ruby programming language, just a few lines of code are needed to be written compared to the other programming languages which may require more lines to obtain the same result. Rails requires minimal software usage so you could efficiently generate more codes. It offers programming convention, which eliminates the use of verbose xml configuration files.

Related Posts :

Diffrence between ruby Gem V Plugins

Topic Ruby Gem Rails Plugin
Definition A Gem is a packaged Ruby application using the packaging system defined by RubyGems. A Rails plugin is a packaged Ruby application that extends the core Rails framework.
Example Has a name (e.g. rake) and a version (e.g. 0.4.16). Rails is also installed as a Gem. Has a name (e.g. Goldspike) and a version (e.g. 1.1.1).
Scope Installed in the Ruby or JRuby installation and is available to all applications run using that interpreter. Installed in a specific Rails app only.
Packaging System Rails gem is installed in jruby-1.0\lib\ruby\gems\1.8\gems\rails-1.2.3 as:<DIR>        bin
<DIR>        builtin
68,465 CHANGELOG
<DIR>        configs
<DIR>        dispatches
<DIR>        doc
<DIR>        environments
307          fresh_rakefile
<DIR>        helpers
<DIR>        html
<DIR>        lib
1,072 MIT-LICENSE
11,969 Rakefile
8,001 README

The lib directory contains all the gem source code.

Goldspike plugin is installed in vendor\plugins\rails-integration directory of the application as: 7,089 build.xml
1,141 LICENSE.txt
<DIR>       plugins
6,675 pom.xml
1,447 README
<DIR>       samples

plugins/goldspike directory consists of

24 init.rb
25 install.rb
<DIR>       lib
549 Rakefile
536 README
<DIR>       tasks
<DIR>       test

The lib directory contains all the plugin source code.

Management Gems are managed on your computer using the gem command. You can install, remove and query gem packages using the gem command. Plugins can be installed using after creating a Rails application and then invoking the command

ruby script\plugin install [url or name of plugin]

Load Path gem command adds the lib directory of your gem to the load path of Ruby. Rails adds the lib directory of plugin in your application’s load path.
Specific Features
  • Bundled Rake tasks that get automatically loaded into your Rakefile
  • An installation hook (install.rb)
  • An application initialization hook for the plugin to inject itself (init.rb)
  • ability to bundle additional generators (for the script/generate script)

What is Scaffolding ?

Scaffolding is a meta-programming method of building database-backed software applications. It is a technique supported by some model-view-controller frameworks, in which the programmer may write a specification that describes how the application database may be used. The compiler uses this specification to generate code that the application can use to create, read, update and delete database entries, effectively treating the template as a “scaffold” on which to build a more powerful application.

There are two ways to produce a scaffold in Ruby on Rails: dynamic scaffolding and scaffold generation.

Dynamic scaffolding

When the line scaffold :model_name is added to a controller, Ruby will automatically generate all of the appropriate data interfaces at run time. Since the API is generated on the fly, the programmer cannot easily modify the interfaces generated this way. Such a simple scaffold is often used for prototyping applications and entering test data into a database.

Scaffold generation

The programmer may also run an external command to generate Ruby code for the scaffold in advance: script/generate scaffold model_name. The generate script will produce files of Ruby code that the application can use to interact with the database. It is somewhat less convenient than dynamic scaffolding, but gives the programmer the flexibility of modifying and customizing the generated APIs.

Metaprogramming

Metaprogramming is the writing of computer programs that write or manipulate other programs (or themselves) as their data, or that do part of the work at compile time that is otherwise done at runtime. In many cases, this allows programmers to get more done in the same amount of time as they would take to write all the code manually.

The language in which the metaprogram is written is called the metalanguage. The language of the programs that are manipulated is called the object language. The ability of a programming language to be its own metalanguage is called reflection or reflexivity.

Reflection is a valuable language feature to facilitate metaprogramming. Having the programming language itself as a first-class data type (as in Lisp or Rebol) is also very useful. Generic programming invokes a metaprogramming facility within a language, in those languages supporting it.

Metaprogramming usually works through one of two ways. The first way is to expose the internals of the run-time engine to the programming code through application programming interfaces (APIs). The second approach is dynamic execution of string expressions that contain programming commands. Thus, “programs can write programs”. Although both approaches can be used, most languages tend to lean toward one or the other.