Letz Talk Ruby and Rails

This is about Ruby and of course Rails. So lets get onto Ruby on Rails wagon.

These are some things that I keep using these days:

To check for empty file field:
@image = Image.create params[:image] unless params[:image][:file_data].kind_of? StringIO

here params[:image][:file_data] is the hash for image to be loaded.
If the file field is blank, for :file_data => #<StringIO:0x677cf1c>

Using joins for finding required records:
* A belongs to B

@items = A.find(:all, :conditions => “bs.name like ‘%#{@to_search_for}%'”,
:joins => “left join bs on b_id = bs.id”)
* A belongs to B and B belongs to C

@items = A.find(:all, :conditions => “cs.name like ‘%#{@to_search_for}%'”,
:joins => “left join bs on b_id = bs.id ” +
“left join cs on c_id = cs.id”)
as, bs and cs being the plural forms of a,b and c and hence used as table names.

How to use javascript to go one step back in history pages:
<a href=”javascript:history.go(-1)”>Back</a>

How to add two submit buttons to one form and perform 2 differenr functions:

<%= form_remote_tag :url => {:action => ‘update_info’, :id => @info} %>
<%= render :partial => ‘form’ %>
<%= submit_tag ‘Add Pro’, :name => ‘pro’ %>
<%= submit_tag ‘Add QA’, :name => ‘qa’ %>
<%= end_form_tag %>

check for two submit tag :name attributes in params in controller and do specific task.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s