Wednesday, 14 October 2015

write a helper method to generate link_to with in li


application_helper.rb

def link url_path, class_name, title
content_tag :li do
link_to title, url_path, title: title, class: 'class_name'
end
end

usage:

in app.html.erb file:

<ul>
<%= link users_path, 'user', 'user' %>
</ul>

that's it....

Convert BSON::ObjectId to string and string to BSON::ObjectId


Convert BSON::ObjectId to string:
1.9.3-p125 :080 > profile = Profile.last.id
=> BSON::ObjectId(‘4fe969dd79216d0af9000002’)
1.9.3-p125 :083 > profile_id = profile.to_s
=> “4fe969dd79216d0af9000002”

Convert string to BSON::ObjectId:
1.9.3-p125 :084 > BSON::ObjectId.from_string(profile_id)
 => BSON::ObjectId(‘4fe969dd79216d0af9000002’)

Wednesday, 7 October 2015

Use of scope with validates_uniqueness_of rails

There is a :scope option that you can use to specify other attributes that are used to limit the uniqueness check:

Eg: I want to validate name should be unique with in the section

validates_uniqueness_of :name, scope: :section_id

Thursday, 10 September 2015

Recapture gem integration steps:


1, add recaptcha gem to gemfile.
gem 'recaptcha', :require => 'recaptcha/rails'
2, the run bundle
bundle install
3, register an app in google recaptcha with following link
https://www.google.com/recaptcha/admin
4, add recaptch.rb with following code in instializers
Recaptcha.configure do |config|
  config.public_key  = 'xxxxxxxxxxxxxxxxxxxc' #site key
  config.private_key = 'xxxxxxxxxxxxxxxy' #secreat key
  config.api_version = 'v2'
end
5, add below tag in sign up form
<%= recaptha_tags %>
6, add below code in regitsration create method
def create
if verify_recaptcha
super
else
build_resource(sign_up_params)
flash.now[:alert] = 'There was an error with the recaptcha code below. Please re-enter the code.'
render :new
end
end

that's it..

Friday, 4 September 2015

Validates Email Field From Controller



class User
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i

if valid_email?(user.email)
user.save
redirect_to root_url, :notice => 'Good email'
else
flash[:error] = 'Bad email!'
render :new
end

private
def valid_email?(email)
email.present? &&
(email =~ VALID_EMAIL_REGEX) &&
User.find_by(email: email).empty?
end
end

Thursday, 3 September 2015

Sublime text2 package control install:



Go to link
https://packagecontrol.io/installation
Copy python code for sublime text2/3

ctrl+`  --> To open console.

Paste python code, then press enter

Restart sublime.

Ex: To install trailing spaces.

Then install package Trailing spaces(like package control)

ctrl+shift+p search for package control press enter.

ctrl+shift+p search for trailing space press enter.

Monday, 31 August 2015

Better Errors To Open In Sublime:


group :development, :test do
gem 'better_errors'
gem 'binding_of_caller'
  gem 'meta_request'
end

$bundle

add better_errors.rb in /config/initializers then add bellow code:

if defined? BetterErrors
  BetterErrors.editor = proc { |full_path, line|
    full_path = full_path.sub(Rails.root.to_s, "/home/saritha/sites/testproj")
    "subl://open?url=file://#{full_path}&line=#{line}"
  }
end

To handle with firefox:

1. First of all download and install sublime url handler patch to handle the url with line number. Download sublime-url-handler.
from https://github.com/algorich/sublime-url-handler/archive/master.zip
2. Goto to development.rb file and add BetterErrors.editor = :sublime

3. Goto firefox and type about:config in url and hit enter

4. Right click and create new property with boolean type with name " network.protocol-handler.expose.subl" set "False".

5. Restart Firefox.

6. Run your rails app and get the error link page, click on error link it will ask open open with if sublime-url-hanlder is shown here well done choose and cheers if not shown than give your sublime executable path here and done.

you can add https://chrome.google.com/webstore/detail/railspanel/gjpfobpafnhjhbajcjgccbbdofdckggg to chrome..this will help in detail work flow..