Home > ruby > Letz Talk Ruby and Rails

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:0×677cf1c>

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.

  1. No comments yet.
  1. No trackbacks yet.