Poll
Simple way to validate email+domain with Ruby on Rails
require ‘resolv’ #”gem install resolv-ipv6favor“
email = “foo@example.com” #test with valid email
split_email = email.split(“@”) #split string with “@”
domain = split_email[1].to_s #get domain name
if email =~ (/\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i)
#checking domain name
Resolv::DNS.open do |dns|
@staus = dns.getresources(domain, Resolv::DNS::Resource::IN::MX)
end
else
errors_add(:email, ‘domain name can not be found.’)
end
Get uniq objects from data in Ruby
Some times we need uniq objects while performing complex operationsand avoid duplicate..def make_uniq_by(&blk)object_set = []self.select do |el|only_uniq_objects = !object_set.include?(data=blk[el])object_set << dataonly_uniq_objectsendend
Thanks …. Feedback and suggestion are welcome..
Resolve Cookie overflow in rails
Sometimes we need long flash notice message which is unable to handle by rails and yields cookies overflow error to over come this patch with this method.
put this …
before filter :make_session_clear
def make_session_clear
rollback = session.data.clone
reset_session
rollback.each { |k, v| session[k] = v unless k.is_a?(String) && k =~ /^as:/ }
end
That’s it you are up and running…..
Hidden Counter in partials
Some time we need counter in inside partials , so rails provides an iteration counter automatically to template which can be easily accessed with partial name and counter means “partial_file_name_counter” thus the easy way to deal with counter in partial files …
Simple Tooltip with Ruby on Rails
1st Step
add this code in app/helpers/application_helper.rb
def tooltip(content, options = {}, html_options = {}, *parameters_for_method_reference)
html_options[:title] = options[:tooltip]
html_options[:class] = html_options[:class] || ‘tooltip’
content_tag(“span”, content, html_options)
end
2nd Step
add code in app/views/foo/page.rhtml
3rd Step
add stylesheet in public/stylesheets/yourstyle.css
cursor: help;
color: green;
border-bottom: 1px dotted green
}
http://rafael.adm.br/p/simple-tooltip-helper-for-ruby-on-rails/
What Open source software ?
Open source software (OSS) began as a marketing campaign for free software. OSS can be defined as computer software for which the human-readable source code is made available under a copyright license (or arrangement such as the public domain) that meets the Open Source Definition. This permits users to use, change, and improve the software, and to redistribute it in modified or unmodified form. It is very often developed in a public, collaborative manner. Open source software is the most prominent example of open source development and often compared to user generated content.
I have gone through ton of code for completing remember me functionality, i sink every time ,last i decided to code my own and i got it. Now you can use it.
You need to make changes only in two files.
1>app/controllers/*.rb (eg. account.rb)
2>app/views/your directory/*.rhtml (eg. login.rhtml)
before_filter :loginformcookies
def loginformcookies
if !cookies[:remember_me_id].blank? && !User.count.zero?
if ( User.find(cookies[:remember_me_id]) and Digest::SHA1.hexdigest(User.find(cookies[:remember_me_id]).email)[4,18] == cookies[:remember_me_code])
params[:username]=User.find(cookies[:remember_me_id].to_i).login
params[:test]=cookies[:remember_me_pass]
end
end
end
def login
case request.method
when :post
if session[:user] = User.authenticate(params[:user_login], params[:user_password])
if params[:rememberme]
userId = session[:user][:id].to_s
cookies[:remember_me_id] = { :value => userId, :expires => 30.days.from_now }
userCode = Digest::SHA1.hexdigest(session[:user][:email])[4,18]
cookies[:remember_me_pass] = {:value =>params[:user_password], :expires => 10.days.from_now}
cookies[:remember_me_code] = {:value =>userCode, :expires => 10.days.from_now}
else
cookies.delete :remember_me_pass
cookies.delete :remember_me_code
cookies.delete :remember_me_id
end
flash[:notice] = “Login successful”
cookies[:is_admin] = “yes”
redirect_back_or_default :controller => “admin/general”, :action => “index”
else
flash.now['notice'] = “Login unsuccessful”
@login = params[:user_login]
end
end
end
In view file just pass that variable which you are getting in filter (loginformcookies).
and have fun.
Auto complete in RoR (Ruby on Rails)
Dive to step 5 if you have already running application.
1) Create rails application
rails app_auto_complete
2) Configure your database and set any table say ‘products’.
Assumes that you have products table and name, detail fields are there in table.
3) For quick go scaffold application with
ruby script/generate scaffold Product name:string detail:text
4) Now you have running application , to start application start application
ruby script/server
5) Now install rails plugin from git repo git://github.com/rails/auto_complete.git
if you have git install please start and get with
git clone git://github.com/rails/auto_complete.git
Put it in ~/vendor/plugins
else download manually from repo.
6) Ok, now we have plugin for auto complete , check if you have included default javascripts in layout
if not please add in layout.
<%= javascript_include_tag :defaults %>
7) Now open file where you want to put auto complete functionality.Say if in ~app/views/product/new.rhtml
replace line <%= f.text_field :name %>
with <%= text_field_with_auto_complete ‘product’, ‘name’,{}, :skip_style => false %>
8) Now open products controller and put this line after
class ProductsController < ApplicationController
skip_before_filter :verify_authenticity_token,
nly => [:auto_complete_for_product_name]
9) Now create method for auto complete
def auto_complete_for_message_to()
@products = Product.find(:all , :conditions = >["name like (?)","%"+params[:product][:name].to_s+”%”])
render :partial => ‘product’
end
10) Step for showing auto compete result.
Create partial file in product folder and name it “_product.rhtml”
and put this code
<ul>
<% for product in @products do %>
<li>
<div><%=h product.name %></div>
</li>
<% end %>
</ul>
11) Finally restart you application and go to url
http://host:port/products/new (For local development)
Have fun !!!!!
For any query please post …..
Deploy Rails (Ruby on Rails )application…
Deploy Rails (Ruby on Rails )application from Windows with Heroku and Git.
1) First get Git and install it.
2) If Git has not set PATH , press window+break and in advance tab set path “C:\Program Files\Git\bin”
3) Restart you system
4) Now install gem Heroku
gem install heroku
5) Make application with rails “appplication”
6) Initialize git repo.
git init
7) Now, Create your application on Heroku
heroku create
This will ask you for heroku credentil email and password once you have given correct will save in heroku
credentil file. /heroku/credentil
If you have already created application on heroku please skip this step.
8) Now it’s time to push you application to Heroku
git push heroku master
9) Now Database on Heroku
heroku rake db:migrate
Hey , your creation is on ^
In case if you need to set your rsa key just type ssh-keygen -t rsa -C “email_address” it will generate rsa key for heroku.You can find in current user ~/.ssh folder.
Have fun !!!!!