Feeds:
Posts
Comments

Defining Subdomains for Local Development

When you begin developing an application using subdomains, you need to move beyond using http://locahost:3000 as we need to be able to develop and test with subdomains. You can open up your /etc/hosts (within a Unix-based O/S) file and add the following.

127.0.0.1 localhost
127.0.0.1 lifestyle.dev
127.0.0.1 life2go.lifestyle.dev
127.0.0.1 vitalzeu.lifestyle.dev

This will let you make request to http://lifestyle.dev:3000 and http://life2go.lifestyle.dev:3000
It's important to remember that the subdomain must be specified here to work for local requests.
Unfortunately hosts file dont support wild cards(*).

Detecting Subdomains in Rails

In the application.rb file add this line to find subdomains
@subdomains = request.subdomains.first

The subdomains method isn't particularly smart, all it does is splitting on.and returning all but the
last two parts. So to get it to work in development then you will need to have the same number
of parts to your domain as you will in production.
So if you'd have life2go.lifestyle.com in production then you will need to have three part domain
names in development too, so something like life2go.lifestyle.dev mapped to your 127.0.0.1 address.

Take a look at the doco for more information.

The request.subdomains.first will work both in production and development environments.

I was Wondering how to use my Rails application to redirect to two URLs .My application should be used on two domains with one server and one database at my back end with single application. I faced this scenario when i was working for an onsite project.

going into details…

when we hit on domain1 say http://www.domain1.com the app should redirect to controller => :home, action => :home an when we hit on domain2 say http://www.domain2.com the app should redirect to controller => :account, :action => :login. This is the actual scenario that we were looking

The major part of the code handles with Routes.rb and Environment.rb.

First write a method in application.rb/application_helper.rb to deferentiate any css/images/links visible on that domains

let us say in application.rb the code looks like

# check Whois Domain Name
# Domain name will be domain1.com or domain2.com based on the request

def check_domain?

if request.domain == “domain1.com”

login_required

elsif request.domain ==”domain2.com”

# login not required

end

and finally add the code in your application to differntiate URls

# in config/routes.rb
map.connect '', :controller => 'domain_tests', :action => 'domain1', :conditions => {:domain => "domain1"}
map.connect '', :controller => 'domain_tests', :action => 'domain2', :conditions => {:domain => "domain2"}

# in config/environment.rb after the initialize section
module ActionController
  module Routing
    class RouteSet
      def extract_request_environment(request)
        { :method => request.method, :domain => request.domain.split('.').first }
      end
    end
    class Route
      alias_method  :o ld_recognition_conditions, :recognition_conditions
      def recognition_conditions
        result = old_recognition_conditions
        result << "conditions[:hostname] === env[:hostname]" if conditions[:hostname]
        result
      end
    end
  end
end

# courtesy of http://www.smallroomsoftware.com/articles/2007/2/10/rails-routing-based-on-hostname

# in app/domain_tests_controller.rb
def domain1
  render :text => "you've hit domain one"
end

def domain2
  render :text => "you've hit domain two"
end

Finally we are done and this works in Production pretty good !!!
Test it in your app's and get me a feedback ..ciao

Amazon has a great feature called EBS which enables to have data persistence in the event of an instance failure.
We have configured mysql to use the EBS for datafiles. Details for this can be found here: http://docs.google.com/Doc?id=dcn2ckbh_21gznbbjhr

Though a great feature, EBS has a couple of operational limitations.
1. It has a cryptic billing structure which bills based on (capacity + usage). Now most of us can’t really predict the usage of disk and risk overshooting this.
2. EBS will be slower that locally mounted storage.

To overcome these issues, in our RoR application, we decided make some changes.

We decided to upload all user data to TWO locations – 1st location is the usual “RoR-app-home/public/” directory. This directory is in the local storage of the instance.
The 2nd location is the EBS (/dev/sdh) mounted on /mnt/data-store of the instance. Within data-store, we created a few directories for storing various types of user data.
1. During upload, the data is copied to both locations. i.e. We write to both local and EBS.
2. During read – we read it from local directory of EC2. This local reads ensures that EBS is not hit with multiple read requests and our EBS costs are low. There is alo the speed benefit of reading from local storage as opposed to reading from EBS (Amazon AWs developers can correct me on the speed issue.)

Here is the sampe code where we are uploading a video and a thumbnail associated with the video: Keep and eye out for “video.rewind”.

Add in app/model/video.rb (for our application – you will have adapt for you app.)

VIDEO_UPLOAD_PATH = “public/video_player/videos/”
THUMBNAIL_UPLOAD_PATH = “public/video_player/thumbnail/”

#Manage the path depending on OS
#Hard disk usage Optimisation for AWS.
#Replicate videos, images, into AWS local hard disk.

VIDEO_UPLOAD_PATH_FOR_AWS = “/mnt/data-store/app-data/videos/”
THUMBNAIL_UPLOAD_PATH_FOR_AWS = “/mnt/data-store/app-data/thumbnails/”

# create and upload video , thumbail
def self.create_video
if valid_video?(video) && valid_thumbnail?(thumbnail)
video_filename = sanitize_attachment_name(video)
thumbnail_filename = sanitize_attachment_name(thumbnail)
@video = self.new do |video|
video.video_name = video_filename
video.thumbnail_name = thumbnail_filename
end
self.upload_video(video, video_filename, @video.id) && self.upload_thumbnail(thumbnail, thumbnail_filename, @video.id) if @video.save
end
end

def self.upload_video(video, video_filename, video_id)
video_path = VIDEO_UPLOAD_PATH + “#{ video_id}_” + video_filename
File.open(video_path, “wb”) { |f| f.write(video.read) }
# code to manage video upload file to EBS
video.rewind
video_path_for_aws = VIDEO_UPLOAD_PATH_FOR_AWS + “#{ video_id}_” + video_filename
File.open( video_path_for_aws, “wb”) { |f| f.write(video.read) }
end

def self.upload_thumbnail(thumbnail, thumbnail_filename, video_id)
thumbnail_path = THUMBNAIL_UPLOAD_PATH + “#{ video_id}_” + thumbnail_filename
File.open(thumbnail_path, “wb”) { |f| f.write(thumbnail.read) }
# code to manage thumbnail image upload file to EBS
thumbnail.rewind
thumbnail_path_for_aws = THUMBNAIL_UPLOAD_PATH_FOR_AWS + “#{ video_id}_” + thumbnail_filename
File.open(thumbnail_path_for_aws, “wb”) { |f| f.write(thumbnail.read) }
end

Playing with auto complete in Firefox is fun all the time , but this is not going to be same in Others Browsers(IE & Safari),
after Googling for many days i came up with a pleasant solution.

Rails2.0: auto_complete is now a plugin (it’s not in the core anymore), you will install it before using it:
script/plugin install auto_complete

Somewhere in your views you’ll want code somewhat like the following.
<%= text_field_with_auto_complete :article, :contains, { :size => 15 }, :skip_style => true %>

I specified a size for the text area with the :size => 15 values in the hash. I also included :skip_style => true which keeps the helper from automatically inlining CSS styles into the page.

and now the major issue i faced with IE and Safari browsers are :
Q. Up, Down arrow keys are not working for auto complete in non-mozilla browsers when drop down list appears
The solution is ..
This is due to a bug in Scriptaculous. At the time of writing you will need to apply the following patch that will fix it:
in controls.js around line 86 you will observe these lines
Element.hide(this.update);
Event.observe(this.element, ‘blur’, this.onBlur.bindAsEventListener(this));
Event.observe(this.element, ‘keydown’, this.onKeyPress.bindAsEventListener(this));
},

and now you modify above lines with the following code of lines :

Element.hide(this.update);
Event.observe(this.element, ‘blur’, this.onBlur.bindAsEventListener(this));
Event.observe(this.element, ‘keypress’, this.onKeyPress.bindAsEventListener(this));
// Observe keydown for non-Mozilla browsers per http://dev.rubyonrails.org/ticket/10126
if (Prototype.Browser.Gecko) {
Event.observe(this.element, ‘keypress’, this.onKeyPress.bindAsEventListener(this));
} else {
Event.observe(this.element, ‘keydown’, this.onKeyPress.bindAsEventListener(this));
}
},
and restart the server ,now the arrow keys will work in both IE and Safari browsers.

After a long time i was stuck on a simple issue . Hope this solution would help for some one searching on google or ruby-api for instant .

Here is the solution for jumbling array elements

Non-optimal Ruby 1.8.6 code :
array.sort_by { rand }

Ruby 1.8.7+ have an optimised version built in:
array.shuffle

Enjoy :(

This similar article may be found on the internet,  but i guess this article will be helpful for newbie’s.

Migrations are a convenient way to alter your database in a structured and organised manner.

Here we are going to see how to create stored procedures with rake db:migrate

First create a new migration file in your db/migrate folder using

ruby script/generate migration stored_procedures. This will create the file db/migrate/001_stored_procedures.

Edit the code to tell it what to do.

The method self.up is used when migrating to a new version, self.down is used to roll back any changes if needed. The class name needs to be the same as the migration name (i.e. db/migrate/001_stored_procedures needs a class name of @StoredProcedures@).

Let us migrate a stored procedure called items with a basic sql code :

The migration file now looks like this :

class StoredProcedures < ActiveRecord::Migration
  def self.up
    execute <<-__EOI
      CREATE DEFINER=`root`@`localhost` PROCEDURE `items`(IN l_item INT,IN userid INT,OUT l_itemid INT,OUT l_item_name VARCHAR)
      BEGIN SELECT itemid,item_name,held_by,id INTO l_itemid,userid,id FROM users_items WHERE itemid=l_item AND held_by=userid;
      END
    __EOI
  end
  def self.down
    execute "DROP PROCEDURE IF EXISTS `items`"
  end
end

And now the cool part is just run rake db:migrate, Rails will create all the migration files in your database and also creates Stored procedures.




Follow

Get every new post delivered to your Inbox.