Rails Notes

Check empty, nil, blank
object.blank?

Formatted JSON
JSON.pretty_generate(my_json)

Pass parameters between new and create actions
Use a hidden field tag
hidden_field_tag :data, @data

Render partial with collection

render :partial => "product", :collection => @products 

Simple search

def self.search(search)
search_condition = "%" + search + "%"
find(:all, :conditions => ['title LIKE ? OR description LIKE ?', _
search_condition, search_condition])
end

Another way
SomeModel.where("field LIKE ?", params[:search]).first

validate presence of one or the other


validates :email, :presence => {:unless => "phone", _
:message => "Please enter either an email or a phone number"}


data types for rails

:string, :text, :integer, :float, :decimal, :datetime, :timestamp, :time, :date, :binary, :boolean.

 add fields to existing table


rails generate migration add_fieldname_to_tablename fieldname:string


test a controller actions using Rails console


app.get '/controller/action'


for passing parameters use

app.post'controller/action?parameter1=value1¶meter2=value2'


for checking the response

app.response


Convert a list in to comma separated string 


tags.map { |tag| tag.name }.join(',')


© 2017. All rights reserved.