Posts

Showing posts with the label rails 4

Create Nested form In Ruby on Rails 4.1

In Rails we create simple form and also sometimes we need nested form. In Rails we can easily do that using  gem “nested_form”. let's create a project using “nested_form”. Before that I would like to ask you to have a look on nested_attribute_documentation(http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html#method-i-accepts_nested_attributes_for). 1) Create a new Rails 4 Project.     rails new nested_app 2) Include the gem in your gem file.   gem "nested_form" 3) run bundle install  bundle install 4) Generate the necessary javascript file.   rails g nested_form:install 5) Include the javascript file in the asset pipeline. in the application.js add the below line.   //= require jquery_nested_form 6) Let say we have a Exam model and one Exam can have multiple Questions.So lets create the Exam model. class Exam < ActiveRecord::Base   has_many :questions, dependent: :destroy   accepts_nested_attributes_for :questions,

How to do rake db:seed in using neo4j gem in rails 4

Neo4j is a graph database, using neo4j gem( https://github.com/neo4jrb/neo4 j) we can easily use neo4j database in our ruby on rails application ( http://programminginrubyonrails.blogspot.in/2014/12/neo-4j-with-rails.html ). Till now neo4j gem does not support rake db tasks. You can check all the supported task by typing rake -T in your console. Now we can create our own custom rake task to seed some  data in neo4j . Step 1: Create a file under lib/tasks name it seed.rake Step 2: Put the below code in seed.rake. namespace :db do   desc 'Load the seed data from db/seeds.rb'   task :seed => :environment do     seed_file = File.join(Rails.root, 'db', 'seeds.rb')     load(seed_file) if File.exist?(seed_file)   end end That's it. now you can run rake db:seed from your terminal.. Simple :) 

Set up Neo 4j With Rails In Ubuntu

How to setup new 4j database with Rails 4. Step 1: Install Neo4j Database in ubuntu.   login as Admin  sudo -i  wget -O - http://debian.neo4j.org/neotechnology.gpg.key | apt-key add -  Add Neo4J to the Apt sources list:   echo 'deb http://debian.neo4j.org/repo stable/' > /etc/apt/sources.list.d/neo4j.list  Update the package manager:  exit as admin  apt-get update  Install Neo4J:  apt-get install neo4j  sudo /etc/init.d/neo4j-service restart Step 2: Create a new rails project.  Rails new neo4j Step 3:  Add neo4j Gem in your gem file   gem 'neo4j', github: 'andreasronge/neo4j' Step 4: Go to application.rb require File.expand_path('../boot', __FILE__) require "rails" %w(   neo4j   action_controller   action_mailer   sprockets ).each do |framework|   begin     require "#{framework}/railtie"   rescue LoadError   end end # Require the gems listed in Gemfile, including any gems # you've

Creating RESTful API using Grape and RABL in Rails 4

We can create a RESTful API using Grape and RABL in Rails4. Step 1: Add this GEMS in your Gem file, And do Bundle install.  gem 'grape'  gem 'rabl-rails'  gem 'grape-rabl'  gem 'rack-cors', :require => 'rack/cors' Step 2: Create a file under lib directory called api.rb, and create a folder inside the view named as api. # lib/api/v1/root.rb class API < Grape::API end Step 3: Add this line to your application.rb file    config.paths.add "app/api", glob: "**/*.rb"     config.autoload_paths += %W(#{config.root}/lib #{config.root}/app/api/*)     config.middleware.use Rack::Cors do       allow do         origins '*'         # location of your API         resource '/*', :headers => :any, :methods => [:get, :post, :options, :put, :delete]       end     end     config.middleware.use(Rack::Config) do |env|       env['api.tilt.root'] = Rails.root.join "app", "

Set up ruby 2.0 and Rails 4.0.1 in Rackspace

First we need to install ruby Step 1)Install Ruby apt-get -y update apt-get -y install build-essential zlib1g-dev libssl-dev libreadline6-dev libyaml-dev cd /tmp wget http://cache.ruby-lang.org/pub/ruby/2.0/ruby-2.0.0-p247.tar.gz tar -xvzf ruby-2.0.0-p247.tar.gz cd ruby-2.0.0-p247/ ./configure --prefix=/usr/local make make install Step 2) Install Rails gem install rails 4.0.1 Step 3) Install passenger gem install passenger Step 4) Install Apache2 apt-get install apache2 Step 5) Install Mysql sudo apt-get install mysql-server mysql-client That's it we are done with the setup.

upgrade to rails 4 and ruby 2

To upgrade your rails version to rails 4 is pretty easy just follow the below steps. - I assume you have already rails installed in your system and also you are using rvm and ruby. - If you are not using rvm i recomend you to use rvm. please check the link for rvm ( http://rvm.io/ ) open a terminal and follow the steps. Step 1:  rvm get head (to get the latest version of rvm) Step 2:  rvm install 2.0.0 (to install ruby) Step 3:  rvm use 2.0.0 Step 4:  rvm gemset create rails4 Step 5:  gem install rails That's it to upgrade your rails version to rails 4 and ruby 2 in ubantu 12.04. Now if you run rails new new_app it will automatically create your rails application in rails 4 simple :) Further if you want to use both rails 3 and and rails 4 you can create a .rvmrc file in your application and mention the gem set in that file. echo "rvm ruby-1.8.7-p352@gemset" > .rvmrc And inside the file mentioned your gem set. like rvm use 2.0.0 rvm gem