Thursday, 26 April 2012

BEST RAILS COMMUNITY - WORKING WITH RAILS




WWR (WORKINGWITHRAILS) One of the best and biggest open community for ruby on rails.  

DSC created the site as an open project and called it WorkingWithRails.  

From here the community itself took over and built the most comprehensive database of Rails workers, companies, enthusiasts and contributors. But rather than an entirely passive system it linked in to the blogs, source code contributions, etc. of the various community members.

You can view the dsc development progress towards the WWR at here http://dsc.net/wwr.html

My complete profile at workingwithrails http://workingwithrails.com/person/10406-uma-mahesh-varma

You too join in that community  and share your views about rails..

Thank You,
Uma Mahesh




How to view log information for the application that was deployed to Heroku


Hi Developers,

Now a days every one is choosing heroku for deploying rails application, as it provides free of cost and its in cloud environment.

As a devloper we are required to view the log file for debugging the things. when a application was deployed to heroku it will be run in the production environment. To view the log information you are going to use 'heroku logs' from the local application directory.

But recently I came to know that we can view log with live stream as same a local application with the below command.

> heroku logs --tail

This will display the log file with live stream with every request that was hit by the server.


Thank You,
Uma Mahesh.

Wednesday, 18 April 2012

jQuery missing : after property id


Hi,

I am sending ajax request from a webpage and getting error in my error console as 'after property id'

Here is my code

  $('#link').click(function(){
                  $.ajax({
                  type : 'get',
                  url : '/myurl',
                  datatype : 'json',
                  async: false,
                  beforeSend: {
                        $("#spinner").show();
                  },
                  headers: {
                       'X-Transaction': 'POST Example',
                       'X-CSRF-Token': $('meta[name="csrf-token"]').attr('content')
                     },
                  success : function(data) {
                  $("#spinner").hide();

                  },
                  error : function(data){
                  $("#spinner").hide();
                  }
                });
       });


after googling I came to see its an issue with the 'beforeSend' parameter.

beforeSend expects a function. You are trying to pass it a pseudo object-literal having an invalid syntax.

Here is the correct syntax:

                  $('#link').click(function(){
                                  $.ajax({
                                  type : 'get',
                                  url : '/myurl',
                                  datatype : 'json',
                                  async: false,
                                  beforeSend: function() {
                                        $("#spinner").show();
                                  },
                                  headers: {
                                       'X-Transaction': 'POST Example',
                                       'X-CSRF-Token': $('meta[name="csrf-token"]').attr('content')
                                     },
                                  success : function(data) {
                                  $("#spinner").hide();

                                  },
                                  error : function(data){
                                  $("#spinner").hide();
                                  }
                                });
                       });

Now the spinner image is displaying fine.

Thank You,
Uma Mahesh.

Monday, 16 April 2012

bootstrap modal disappear immediately after showing


Hi,

I have used bootstrap frameowrk in my project for the javascript effects. In my application there is a register button, when clicked on should display model window. But when I clicked on Register button, modal window appears and disappear again. I Have googled for many resource and finally found the solution which is a simple one.

As my application in rails 3 it uses assets pipeline. Where we need to place all the javascript files inside the 'assets/javascripts' folder. When I load the page it will fetch all the javascript files from the javascript folder.


In those files, I was loading both bootstrap.js AND bootstrap-modal.js.

bootstrap.js includes bootstrap-modal.js.

So I have removed the bootstrap.js file and every thing works fine for me.


Thank You,
Uma Mahesh.

$.fn.tooltip is undefined


Hi,

I have used bootstrap frameowrk in my project for the javascript effects. I used the get '$.fn.tooltip is undefined' error in the error console for the tooltip functionality.

I came to see its been due to configuration of javascript files. Some javascript plugins depends on other javascript files as dependencies.

Popovers require the tooltip plugin as a dependency. Have you included the tooltip plugin as well on your page, before including the popover plugin?

Here is the reference url where I have got the solution.

URL: https://github.com/twitter/bootstrap/issues/1513

So by changing the order of the javascript files, I have fixed the issue.


Thank You,
Uma Mahesh.

$ is not defined in the javascript error console


I have implemented the jqeury onclick event as below

$(function () {
            $('#radio_button').click(function(){
                     alert('alert me');
                     $.ajax({
                            type : 'get',
                            url : '/check_things?format=json',
                            datatype : 'json',
                            headers: {
                                 'X-Transaction': 'POST Example',
                                 'X-CSRF-Token': $('meta[name="csrf-token"]').attr('content')
                               },
                            success : function(data) {
                            },
                            error : function(data){
                            }
                          });
                 });

          });


When I load the page, I used to get '$ is not defined' in the Error console of my firefox browser.

After making some research, I came to see its been due to issue with jquery files.

We need to load the required jquery files before the script functional call. So I have copied the javascript files from the bottom of the page to header part of the page.

Now the Error console was clear and I am able to get the alert message when I click on the radio button.


Thank You,
Uma Mahesh

Sunday, 15 April 2012

PostgreSQL psql: could not connect to server: Connection refused



I have faced issue with my rails application whcih uses postgresql as the database. Here is the error I have received when I start my server.


/usr/local/rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.2.0/lib/active_record/connection_adapters/postgresql_adapter.rb:1161:in `initialize': could not connect to server: Connection timed out (PG::Error)
Is the server running on host "168.162.1.34" and accepting
TCP/IP connections on port 5432?


I came to see this is due to postgresql configuration issue. Recently my Ip has been changed to new one and need to modify the IP address with the new one in the postgresql configuration file.

postgresql configuration file exists at '/etc/postgresql/9.1/main/postgresql.conf'

You need to have root access to modify this file. Login as root user through terminal and go to the above mentioned path.

> gedit postgresql.conf

which will open the configuration file in edit mode in the gedit editor.

> listen_addresses = '168.162.5.34'

Modify the above line with your desired IP address.

save the file abd exit from the terminal from edit mode.

restart the server by below command

> sudo /etc/init.d/postgresql restart

By this way you can change the configuration of postgresql.

Thank You,
Uma Mahesh Varma.

Thursday, 12 April 2012

pasing css class parameter to form_tag


Hi,

form_tag is mainly used where you don’t need to attach a form to a model instance. If you need model based form you need to use form_for.

You need to pass css class as a parameter to form_tag. But it should be done as below.

= form_tag({:url => ''},:class => 'form-horizontal') do |f|


Thank You,
Uma Mahesh.

Wednesday, 11 April 2012

Disable ssl in developement environment


Hi,

I am using 'bartt-ssl_requirement' gem for ssl(https) functionality in my rails application.

But working in development environment https is not working. So We need to disable ssl in development. This can be done in many ways.
But the simplest way of doing this is by making the ssl disabled at configuration level.

Add the below line of code in the development environment.

SslRequirement.disable_ssl_check = true

Restart the server and check now. In the development environment ssl is disabled.


Thank You,
Uma Mahesh.

Tuesday, 10 April 2012

Active Record Queries in Rails 3


Hi,

Rails 3 has Introduced many methods in ActiveRecord.

Here is the list of Active Record Queries in Rails 3 that I have used in my daily application developement.

    subscriptions = Subscription.where(
        :renewal => (Date.today-6)..(Date.today),
        :status => 'pending',
        :retry_count => 0..3,
        :retry_date => NIL )

 
   In the above query, 'renewal' column is a date datatype. By running query with (Date.today-15)..(Date.today) will fetch the records inbetween those dates.



Thank You,
Uma Mahesh    



Creating CSV file from postgres sql command


Hi,

Here is the command to create a CSV from postgres sql.
By running a single command you can get the CSV file with the specific select statement,

COPY (SELECT first_name,last_name FROM users) TO ‘/tmp/users.csv’ WITH CSV HEADER

Thank You,
Uma Mahesh.

command to dump data to dump file in postgres


Hi,

Here is the command to dump data of a database to dump file

pg_restore --verbose --clean --no-acl --no-owner -h localhost -U postgres -d aff_2012_04_09 latest.dump



Thank You,
Uma Mahesh.

Finding null records from date datatype column in postgres database


Hi,

While working with Postgres I had faced issue with query with date data type. When I try to fetch records with date column as null as below

subscriptions = Subscription.where(:retry_date => ' ' )

I have got the below error

PG::Error: ERROR: invalid input syntax for type date:

I came to see we cannot find the records through Active record as above.

By googling I found the way to fetch the records as below

subscriptions = Subscription.where(:retry_date => NIL )

Thank You,
Uma Mahesh.

Wednesday, 4 April 2012

Error on Devise Registrations controller spec


Hi,

When I run the registrations controller spec file to test the registration page of the devise. I am getting the below error

ruby -I spec spec/controllers/registrations_controller_spec.rb
F

Failures:

  1) RegistrationsController Display Registration page returns http success
     Failure/Error: get 'new'
     AbstractController::ActionNotFound:
       Could not find devise mapping for path "/users/sign_up".
       Maybe you forgot to wrap your route inside the scope block? For example:
     
       devise_scope :user do
         match "/some/route" => "some_devise_controller"
       end
     # spec/controllers/registrations_controller_spec.rb:7:in `block (3 levels) in <main>'

Finished in 0.2411 seconds
1 example, 1 failure

Failed examples:

rspec spec/controllers/registrations_controller_spec.rb:6 # RegistrationsController Display Registration page returns http success

running specific spec file through console

Here is the command:

> ruby -I spec spec/models/user_spec.rb

rails generate


rails generate
Usage: rails generate GENERATOR [args] [options]

General options:
  -h, [--help]     # Print generator's options and usage
  -p, [--pretend]  # Run but do not make any changes
  -f, [--force]    # Overwrite files that already exist
  -s, [--skip]     # Skip files that already exist
  -q, [--quiet]    # Suppress status output

Please choose a generator below.

Rails:
  assets
  controller
  generator
  helper
  inherited_resources_controller
  integration_test
  mailer
  migration
  model
  observer
  performance_test
  resource
  responders_controller
  scaffold
  scaffold_controller
  session_migration
  task

ActiveAdmin:
  active_admin:assets
  active_admin:devise
  active_admin:install
  active_admin:resource

ActiveRecord:
  active_record:devise

Coffee:
  coffee:assets

Cucumber:
  cucumber:install

Devise:
  devise
  devise:install
  devise:views

EmailSpec:
  email_spec:steps

Erb:
  erb:controller
  erb:mailer
  erb:scaffold

FactoryGirl:
  factory_girl:model

Formtastic:
  formtastic:form
  formtastic:install

Jquery:
  jquery:install

Js:
  js:assets

Kaminari:
  kaminari:config
  kaminari:views

Mongoid:
  mongoid:devise

Paperclip:
  paperclip

Responders:
  responders:install

Rspec:
  rspec:controller
  rspec:helper
  rspec:install
  rspec:integration
  rspec:mailer
  rspec:model
  rspec:observer
  rspec:scaffold
  rspec:view

TestUnit:
  test_unit:controller
  test_unit:helper
  test_unit:integration
  test_unit:mailer
  test_unit:model
  test_unit:observer
  test_unit:performance
  test_unit:plugin
  test_unit:scaffold