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

Wednesday, August 21, 2013

Rails 4 - Scope

In rails 4, scopes should take a proc object or a block. Take an example this User model
Class User < ActiveRecord::Base
  attr_accessor :name,:age,:status

  def initialize(name, age)
    @name = name
    @age = age
  end
end
I would like to make default scope based on status 'user' and admin scope based on status 'admin'. In rails 3, I can make scope like this
#Rails 3
scope :admin, where(status: 'admin')
default_scope where(status: 'user')

Rails 4, I can make scope this way
#Rails 4
default_scope { where(status: 'user') } #  block
#default_scope ->{ where(status: 'admin) } #Proc valid
scope :admin, ->{ where(status: 'admin') }
If we try to use scope without(see rails3 examples) passing proc or block in Rails 4, it triggers DEPRECATION WARNING message.

Rails 4 - Finders

Old-Style finders are deprecated
In Rails 4, Old-Style finders are deprecated. Consider User model with name, age members
Class User < ActiveRecord::Base
  attr_accessor :name,:age

  def initialize(name, age)
    @name = name
    @age = age
  end
end

In this class, If we want find user with name of 'foo'. See the difference in rails 3 and rails 4 call
#Rails 3
User.find(:all, conditions: { name: 'foo' })
#Rails 4
User.where(name: 'foo')

In rails 4, you will get DEPRECATION WARNING: Calling #find(:all) is deprecated. You can call #all directly instead or build a scope instead of using finder options.

Dynamic finders that return collections are deprecated
#Rails 3
User.find_all_by_name('foo')
#Rails 4
User.where(name: 'foo')

In Rails 4 alerts DEPRECATION WARNING if we use dynamic method(User.find_all_by_name). Preferable alternative option is User.where(...).all

Tuesday, November 20, 2012

Rails 3.2.9

Rails team announced it's new release 3.2.9. It includes changes/bug fixes in Action Pack, Active Model and etc.

Have a look more information here

Monday, September 3, 2012

Could not find a JavaScript runtime. See https://github.com/sstephenson/execjs for a list of available runtimes. (ExecJS::RuntimeUnavailable)

$ rails server

/home/user/.rvm/gems/ruby-1.9.3-p194/gems/execjs-1.4.0/lib/execjs/runtimes.rb:51:in `autodetect': Could not find a JavaScript runtime. See https://github.com/sstephenson/execjs for a list of available runtimes. (ExecJS::RuntimeUnavailable)
             from /home/user/.rvm/gems/ruby-1.9.3-p194/gems/execjs-1.4.0/lib/execjs.rb:5:in `<module:ExecJS>'
 
The reason for this issue is missing Node JS Package Manager. Check this link for how to install package manger based on your operating system.

https://github.com/joyent/node/wiki/Installing-Node.js-via-package-manager

I hope it may help you to solve the problem.

The program 'rails' is currently not installed. You can install it by typing: sudo apt-get install rails

user@localhost:~$ rails new blog
The program 'rails' is currently not installed.  You can install it by typing: sudo apt-get install rails

If you get this message when you were creating new rails project. To fix this issue

1. Go to profile settings (.bashrc or .bash_profile)

2. Add this line at end of the file

[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # Loading RVM into a shell

3. If you are in existing shell prompt, execute this command

source ~/.bashrc
or
source ~/.bash_profile

If you open a new terminal, you can notice  that rails command is automatically available.I hope this post helpful to fix rails command issue.



Note:
I believe you have installed ruby by Ruby Version Manger(RVM). Refer the installation steps
https://rvm.io/rvm/install/


Friday, July 27, 2012

Rails 3.2.7!

Rails Team has been announced version 3.2.7. It contains important security fixes(see here), so update quickly.

Source code & change logs are available here
https://github.com/rails/rails/compare/v3.2.6...v3.2.7

Reference:
http://rubyonrails.org/

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. 


Tuesday, January 3, 2012

Install and Setup Ruby, Rails 3 on Ubuntu 11.04

To install Rails 3, system needs supporting packages(including ruby,rake,libtool,etc).

1. Install those support packages 

sudo apt-get install ri ruby ruby1.8 ruby-dev rake libruby1.8 zlib1g-dev libssl-dev libreadline5-dev libncurses5-dev build-essential curl git-core git-gui gitk libxml2 libxml2-dev libxslt1-dev bison autoconf libtool

2. Installing rails by RVM(Ruby Version Manager)
For more information about installing RVM, Check it here

sudo rvm install 1.9.2

To make version 1.9.2 as default ruby version(if you have multiple version ruby)

sudo rvm use 1.9.2 --default

3. Install rails gem

sudo gem install rails

4. Install sqlite3

To install this you may need "libsqlite3-dev". so get this from Ubuntu package repo 

sudo apt-get install libsqlite3-dev

Now rails 3 is ready for developing application, In general rails come up with in-build WEBrick webserver. So you can create and deploy sample application on WEBrick.