Showing posts with label Rails 3. Show all posts
Showing posts with label Rails 3. Show all posts

Wednesday, November 21, 2012

Rails 3.x - Undoing commands

Controller and Action

In rails 3.x, To create a controller with actions we use this command

$ rails generate controller UserController add edit

If you want to remove the above controller and the actions

$ rails destroy controller User add edit

Model

To create a model in rails 3


$ rails generate model User name:string mobile:string

This can be undone using

$ rails destroy model User

Database migration and rollback

To migrate the state of the database using


$ rake db:migrate

We can undo a single migration by this command

$ rake db:rollback

To go all the way back to the beginning by this command

$ rake db:migrate VERSION=0

These commands may be helpful for reverting controller, model and database migration specific changes.

Monday, July 30, 2012

Rails 3 - Bundler

Bundler

Bundler is used to manage your application's gem dependencies. By default all Rails 3 application contains bundler gem.

If your applications need gems other than those belonging to Rails itself, you'll need to specify those gems into manifest file named Gemfile into the root of your Rails project directory.

Simple syntax to load gem

gem "paperclip"
gem "rspec"

Syntax to load gem on specific application environment

group :development, :test do
   gem "rspec"
end

Syntax to load specific gem version

gem "rspec" , "1.2"
gem "rspec", "> 1.2"

Syntax to load gem form a Git Repository

gem "paperclip", :git => "git://github.com/thoughtbot/paperclip.git"

Installing Gems

Once you update the Gemfile then make sure you have installed all the dependencies in your Gemfile of Rails 3 applications.

$ bundle install
$ bundle install --without test

These commands will install all gem dependencies on your Rails 3 application which is specified on Gemfile.

Packing Gems

You can package up all your gems in the vendor/cache directory inside of your Rails 3 applications.

Here is the syntax to package up

$ bundle package

I hope you have some idea about Bundler and how it helpful in Rails 3 applications. If you need any clarifications please let me know.

Saturday, June 23, 2012

Rails 3 tips - Create project with different database(MySQL,PostGresSQL,JDBC,SQL SERVER, etc)

Here are commands to create Rails 3 application with different database

MySQL
rails new app --database=mysql

ORACLE
rails new app --database=oracle

PostgreSQL
rails new app --database=postgresql

SQLite3
rails new app --database=sqlite3

FrontBase
rails new app --database=frontbase

IBM DB
rails new app --database=ibm_db

SQL SERVER
rails new app --database=sqlserver

JDBC
rails new app --database=jdbc

JDBC MySQL
rails new app --database=jdbcmysql

JDBC PostgreSQL
rails new app --database=jdbcpostgresql

JDBC SQLite3
rails new app --database=jdbcsqlite3

Sunday, May 20, 2012

Rails 3 - Routing

Purpose of rails router is that understanding requesting URLs and dispatches them to a controller's action.

Rails 3 introduced new routing DSL which is slight different than rails 2

Examples:

1. Simple route to welcome page which is handled by index action in shops controller

# Rails 2
map.connect 'welcome', :controller => 'shops', :action => 'index'

# Rails 3
match 'welcome' => 'shops#index'

2. Resources - which allows us to quickly declare all of the common routes for a given resourceful controller

# Rails 2

map.resources :products

# Rails 3

resources :products
It creates different routes in your application for Products controller. The routes are /products,/products/new,/products/:id,/products/:id/edit,etc.

3. Namespaces and Routing

Suppose if you want to organize group of controller under a namespace, Example scenario would be mapping admin specific controllers under admin namespace.

# Rails 2

map.namespace :admin do |admin|
  admin.resources :controller => 'orders'
end

# Rails 3
namespace :admin do
  resources :orders
end

In this case you can access admin specific controllers by /admin/orders, etc

4. Simple ROOT mapping on rails 2 and rails 3

# Rails 2

map.root :controller => 'stores', :action => 'index'

# Rails 3

root :to => 'stores#index'

Stores's index action will be called if you access web application directly - http://example.com

Friday, May 11, 2012

An error occured while installing rmagick - Rails 3 Gem(Ubuntu 11.10)

Issue while installing rmagick gem:

An error occured while installing rmagick (2.13.1), and Bundler cannot continue.
Make sure that `gem install rmagick -v '2.13.1'` succeeds before bundling.

Solution would be installing rmagick code library.

Possible solution for this issue would be installing postgresql-server for building a server-side extension or libpq-dev for building a client-side application
$ sudo apt-get install libmagickcore-dev libmagickwand-dev

$ sudo apt-get install libmagickcore-dev libmagickwand-dev


Wednesday, May 9, 2012

Ruby/Rails 3 with NetBeans IDE 7.1.2


This post help you to install Rails 3/Ruby Plugin for NetBean IDE 7.1.2.
Nice Stuff i noticed after install this plugin
1. Open you're existing rails 3 project
2. Open new rails 3 project
3. Run/Debug Rake Task
4. Ruby Shell
5. Rails Console
6. Migrate database
7. Run
8. Debug,etc

I hope it makes rails development faster.

Monday, May 7, 2012

Install & setup Passenger module on Apache2 - Ubuntu 11.10

This guide help you to deploy Rails 3 application on apache2.

1. Install passenger gem on you're machine
$ sudo gem install passenger

2. Install apache2 module for passenger
$ sudo passenger-install-apache2-module

It does following three steps

a. The Apache 2 module will be installed for you.

If any software not available on you're server to install apache 2 module. It will guide you about missing software and some suggestion how to install those missed software

Once all support software available, then it compile & install apache 2 module.

b. Guide you how to configure apache

Once apache2 module installed, it guide you to configure apache - display LoadModule, RailsSpawnServer and RailsRuby location which will be required to configure passenger module on apache(httpd.conf).Just copy and paste those lines to httpd.conf

c. Guide you how to deploy a Ruby on Rails application
<VirtualHost *:80>

ServerName www.yourhost.com

DocumentRoot /somewhere/public

</VirtualHost>

add this virtualhost configuration on httpd.conf file

Now i hope you're apache2 server ready to deploy rails 3 application. it use passenger module for apache2.

Saturday, April 21, 2012

An error occured while installing pg (0.13.2), and Bundler cannot continue - Rails 3 & PostgreSQL issues


"An error occured while installing pg (0.13.2), and Bundler cannot continue.
Make sure that `gem install pg -v '0.13.2'` succeeds before bundling"

Reason for this error could be "failed to build gem native extension", so make sure you have installed PostgreSQL headers properly.

For Ubuntu/Linux flavor, Use following command to install PostgreSQL client and development package

sudo apt-get install libpq-dev

Hope it may helpful to rectify you're problem with rails 3 application with PostgreSQL.

Thursday, March 15, 2012

Good Ruby on Rails Resources

These reference resources are very helpful and make you good in development and understand of  Ruby on Rails.

Official Rails Doc

General reference for Rails
Rails API docs with usage notes

Release - Rails 3.2.2

Rails 3.2.2 has been released. it contains two major security fixes and lots bug fixes.

Some of the major changes are

* Log files are always flushed

* Failing tests will exit with nonzero status code

* Elimination of calls to deprecated methods

* Query cache instrumentation includes bindings in the payload

* Hidden checkbox values are not set if the value is nil

* Various Ruby 2.0 compatibility fixes

More information about source and change log are available here

If you want to try latest rails on you're app, follow install and setup instructions which is available on official rubyonrails.org.

Sunday, February 12, 2012

Rails 3.2.1 has been released


Rails 3.2.1 has been released, with some fixes and doc improvements. 


If you would like to know more information and changes, have a look CHANGELOGs gist.

Sunday, January 29, 2012

RSpec 2.8

RSpec 2.8 has been released, along with rspec-rails 2.8.1 for the full Rails 3.x integration experience.

Saturday, January 28, 2012

Rails 3.2.0 released


It includes Routing & Faster dev mode, explain queries, tagged logger, store.

There’s a v3.2.0 tag on Github and 3-2-stable branch as well. You can see all the glorious details of everything that was changed in our CHANGELOG compilation.

For documentation,  refer 3.2 release notes with upgrade instructions, both the API docs and the guides have been generated for 3.2 as well, and there’s a brand new 3.2-compatible version of Agile Web Development with Rails.

Saturday, January 14, 2012

ActionController::RoutingError (No route matches [GET] "/assets/scaffold.css"):

In rails 3 applications, if any application doesn't refer static resources(js,images,css) such as

ActionController::RoutingError (No route matches [GET] "/assets/scaffold.css")
ActionController::RoutingError (No route matches [GET] "/assets/defaults.js")
ActionController::RoutingError (No route matches [GET] "/assets/logo.png")

Fix for those issues would be

Stylesheets will be in app/assets/stylesheets
Images will be in app/assets/images
javascripts will be in app/assets/javascripts


Thursday, January 5, 2012

Rails 3 - Developing simple app in few minutes


Installing rails 3 - Refer this post for install instructions

1. Create a application call "demo"

$ rails new demo
      create
      create  README
      create  Rakefile
      create  config.ru
      create  .gitignore
      create  Gemfile
      create  app
      create  app/assets/images/rails.png
      create  app/assets/javascripts/application.js
      create  app/assets/stylesheets/application.css
      create  app/controllers/application_controller.rb
      create  app/helpers/application_helper.rb
      create  app/mailers
      create  app/models
      create  app/views/layouts/application.html.erb
      create  app/mailers/.gitkeep
      create  app/models/.gitkeep
      create  config
      create  config/routes.rb
      create  config/application.rb
      create  config/environment.rb
      create  config/environments
      create  config/environments/development.rb
      create  config/environments/production.rb
      create  config/environments/test.rb
      create  config/initializers
      create  config/initializers/backtrace_silencers.rb
      create  config/initializers/inflections.rb
      create  config/initializers/mime_types.rb
      create  config/initializers/secret_token.rb
      create  config/initializers/session_store.rb
      create  config/initializers/wrap_parameters.rb
      create  config/locales
      create  config/locales/en.yml
      create  config/boot.rb
      create  config/database.yml
      create  db
      create  db/seeds.rb
      create  doc
      create  doc/README_FOR_APP
      create  lib
      create  lib/tasks
      create  lib/tasks/.gitkeep
      create  lib/assets
      create  lib/assets/.gitkeep
      create  log
      create  log/.gitkeep
      create  public
      create  public/404.html
      create  public/422.html
      create  public/500.html
      create  public/favicon.ico
      create  public/index.html
      create  public/robots.txt
      create  script
      create  script/rails
      create  test/fixtures
      create  test/fixtures/.gitkeep
      create  test/functional
      create  test/functional/.gitkeep
      create  test/integration
      create  test/integration/.gitkeep
      create  test/unit
      create  test/unit/.gitkeep
      create  test/performance/browsing_test.rb
      create  test/test_helper.rb
      create  tmp/cache
      create  tmp/cache/assets
      create  vendor/assets/stylesheets
      create  vendor/assets/stylesheets/.gitkeep
      create  vendor/plugins
      create  vendor/plugins/.gitkeep
         run  bundle install
Fetching source index for http://rubygems.org/
Using rake (0.9.2.2)
Using multi_json (1.0.4)
Using activesupport (3.1.3)
Using builder (3.0.0)
Using i18n (0.6.0)
Using activemodel (3.1.3)
Using erubis (2.7.0)
Using rack (1.3.6)
Using rack-cache (1.1)
Using rack-mount (0.8.3)
Using rack-test (0.6.1)
Using hike (1.2.1)
Using tilt (1.3.3)
Using sprockets (2.0.3)
Using actionpack (3.1.3)
Using mime-types (1.17.2)
Using polyglot (0.3.3)
Using treetop (1.4.10)
Using mail (2.3.0)
Using actionmailer (3.1.3)
Using arel (2.2.1)
Using tzinfo (0.3.31)
Using activerecord (3.1.3)
Using activeresource (3.1.3)
Using ansi (1.4.1)
Using bundler (1.0.21)
Using coffee-script-source (1.2.0)
Using execjs (1.2.13)
Using coffee-script (2.2.0)
Using rack-ssl (1.3.2)
Using json (1.6.4)
Using rdoc (3.12)
Using thor (0.14.6)
Using railties (3.1.3)
Using coffee-rails (3.1.1)
Using jquery-rails (1.0.19)
Using rails (3.1.3)
Using sass (3.1.12)
Using sass-rails (3.1.5)
Using sqlite3 (1.3.5)
Using turn (0.8.2)
Using uglifier (1.2.1)
Your bundle is complete! Use `bundle show [gemname]` to see where a bundled gem is installed.

2. Go to app folder

$ cd demo

3. Generate a controller called Index with method init

$ rails generate controller Index init
      create  app/controllers/index_controller.rb
       route  get "index/init"
      invoke  erb
      create    app/views/index
      create    app/views/index/init.html.erb
      invoke  test_unit
      create    test/functional/index_controller_test.rb
      invoke  helper
      create    app/helpers/index_helper.rb
      invoke    test_unit
      create      test/unit/helpers/index_helper_test.rb
      invoke  assets
      invoke    coffee
      create      app/assets/javascripts/index.js.coffee
      invoke    scss
      create      app/assets/stylesheets/index.css.scss

4. Start WEBrick server

$ rails server
=> Booting WEBrick
=> Rails 3.1.3 application starting in development on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
[2012-01-04 23:24:05] INFO  WEBrick 1.3.1
[2012-01-04 23:24:05] INFO  ruby 1.9.2 (2011-07-09) [i686-linux]
[2012-01-04 23:24:05] INFO  WEBrick::HTTPServer#start: pid=3220 port=3000

Now app can be accessible by browser. Further you can add/modify controller, view and functionality

This is very simple app.it may take few mins.