Saturday, January 24, 2009

Inline file upload - iframe - ajax - Ruby on Rails

In View

<% form_for(@images, :url => formatted_images_path(:format => 'js'), :html => { :multipart => true,:target => 'upload_frame' }) do |f| %>
Image
<%= f.file_field :uploaded_data, :size => "50" %>
<%= f.submit :Submit %>
<% end %>
< id="'upload_frame'," name="upload_frame" style="width:1px;height:1px;border:0px" src = "about:blank">

in controller
def create
@image = Image.new
if @image.save
respond_to do |format|
format.js do
responds_to_parent do
render :update do |page|
page.replace_html 'imgupload', :partial => 'imageupload',:object => @images
page.visual_effect :highlight, "image_#{@image.id}"
end
end
end
end
end
end

Watermarking an image in Rails - Rmagick

class Image < ActiveRecord::Base

require 'RMagick'
has_attachment :content_type => :image,
:storage => :file_system,
:max_size => 4.megabytes,
:resize_to => '640x400>',
:thumbnails => { :thumb => '100x100>',:medium => '250x150' }

validates_as_attachment

def watermark_image
dst = Magick::Image.read("#{RAILS_ROOT}/public/#{self.public_filename}").first
src = Magick::Image.read("#{RAILS_ROOT}/public/images/logo.gif").first
result = dst.composite(src, Magick::CenterGravity, Magick::HardLightCompositeOp)
result.write("#{RAILS_ROOT}/public/#{self.public_filename}")
end

Improving Rails Applications performance

I have done some research about improving Rails Applications performance. I would feel the the following few points need to be considered when we optimize rails applications for performance:

1) Avoid the use of dynamic URL generation (link_to, url_for) since rails needs to look up the routes table and that may take time. Just hard code the controller name and the action.

2) Try to avoid the excess use of helpers since it adds overhead.

3) You may consider to use Rails Bench to do some testing for you rails application performance.

4) You may consider to use memcached to cache your model and library computation results.

5) try to optimize your database queries. If you use ActiveRecord find, be careful from computation intensive sql queries that returns to you a lot of data that you may not need. The method find may run many select statements for you.

Wednesday, September 24, 2008

observe_field Example - Ruby on Rails

<table>
<tr>
<td><label for="searchtext"><font size="1"><b>Live Book Search:</b></font></label></td>
<td><%= text_field_tag :searchtext %></td>
<td><img alt="spinner" id="spinner" src="/images/spinner.gif" style="display:none;" /></td>
</tr>
</table>

<%= observe_field 'searchtext', :frequency => 0.5,
:update => 'search_hits',
:loading => "Element.show('spinner')",
:complete => "Element.hide('spinner')",
:url => { :controller => 'books', :action=> 'live_search' },
:with => "'search=' + escape(value)" %>

<div id="search_hits">
<table>
<tr>
<th>Name</th>
<th>Author</th>
<th>Price</th>
<th>Desc</th>
</tr>

<% for book in @books %>
<tr>
<td><%=h book.name %></td>
<td><%=h book.author %></td>
<td><%=h book.price %></td>
<td><%=h book.desc %></td>
<td><%= link_to 'Show', book %></td>
<td><%= link_to 'Edit', edit_book_path(book) %></td>
<td><%= link_to 'Destroy', book, :confirm => 'Are you sure?', :method => :delete %></td>
</tr>
<% end %>
</table>

For more detail go to
Wiki.rubyonrails.org

Variables In Request Env - Ruby on Rails

request.env is a Ruby Array that contains information about a visiting user’s and server environments.
request.env is accessible on all pages hosted in a ruby on rails site. The Array contains the following key/value pairs:

  • SERVER_NAME
  • PATH_INFO
  • REMOTE_HOST
  • HTTP_ACCEPT_ENCODING
  • HTTP_USER_AGENT
  • SERVER_PROTOCOL
  • HTTP_CACHE_CONTROL
  • HTTP_ACCEPT_LANGUAGE
  • HTTP_HOST
  • REMOTE_ADDR
  • SERVER_SOFTWARE
  • HTTP_KEEP_ALIVE
  • HTTP_REFERER
  • HTTP_COOKIE
  • HTTP_ACCEPT_CHARSET
  • REQUEST_URI
  • SERVER_PORT
  • GATEWAY_INTERFACE
  • QUERY_STRING
  • REMOTE_USER
  • HTTP_ACCEPT
  • REQUEST_METHOD
  • HTTP_CONNECTION
For testing purposes it might be useful to print out a table with the request.env information in it.

<table>
<th>key</th>
<th>Value</th>
<% for item in request.env %>
<tr>
<td><%= item[0] %></td>
<td><%= item[1] %></td>
</tr>
<% end %>
</table>

Installing Rmagick on Windows Using Ruby 1.8.6

Rmagick tends to be tricky to install, but on windows its relatively simple, except for the fact that if you freshly installed ruby 1.8.6, ruby gems needs to be updated. If you install ruby 1.8.6 and then go to ruby.forge and download rmagick for windows. Extract the zip folder, run the .exe, then open a command line, cd (change directory) to the install folder and do “gem install rmagick —local”. The first time you do this, you might run into the “Error installing gem RMagick (version) .gem[.gem]: buffer error”. To fix that run “gem update —system” with double hyphens and then run “gem install rmagick —local” and everything should now install fine.

Dynamically add and remove html elements using javascript

<input type="hidden" value="0" id="theValue" />
<p><a href="javascript:;" onclick="addElement();">Add Some Elements</a></p>
<div id="myDiv"> </div>

<script>
function addElement() {
var ni = document.getElementById('myDiv');
var numi = document.getElementById('theValue');
var num = (document.getElementById('theValue').value -1)+ 2;
numi.value = num;
var newdiv = document.createElement('div');
var divIdName = 'my'+num+'Div';
newdiv.setAttribute('id',divIdName);
newdiv.innerHTML = 'Element Number '+num+' has been added! <a href=\'#\' onclick=\'removeElement('+divIdName+')\'>Remove the div "'+divIdName+'"</a>';
ni.appendChild(newdiv);
}

function removeElement(divNum) {
var d = document.getElementById('myDiv');
var olddiv = document.getElementById(divNum);
d.removeChild(olddiv);
}
</script>

Contact Me for any help regarding rails/nodejs/php/mysql

Name

Email *

Message *