escape japaneese text in url
when the file names were actually in japaneese, IE by default will not converting the links from Japanese text to unicode URL format i.e %20. Here is the fix that required
<a href="<%= product.product_image.url) %> ">SOME FILE NAME</a>
CHANGE TO :
<a href="<%= URI.escape(product.product_image.url) %>" >SOME FILE NAME</a>
install cisco vpn client on ubuntu
Ubuntu ships by default with the plugin for the Point-to-Point Tunneling Protocol (PPTP), but we need the plugin for the Cisco Compatible VPN (vpnc), that provides easy access to Cisco Connector based VPNs. To install the vpnc plugin, open your terminal and run:
Open Ubuntu Software Center and install Openconnect
sudo apt-get install vpnc
sudo apt-get install network-manager-vpnc
add sublime text option to the contextual menu
After installing sublime text, we must be able to open text files with it. Right click on a text file and you must see the option "open with sublime text". In case you don't , click Open with other applications and you should see Sublime text option there. No luck yet, here are the troubleshooting steps.
# open the .desktop file for sublime
sudo sublime /usr/share/applications/sublime.desktop
# add the below lines to the file
[Desktop Entry]
Version=1.0
Name=Sublime Text 2
# Only KDE 4 seems to use GenericName, so we reuse the KDE str...
know which window manager is running
In ubuntu, you have a variety of window managers to choose from. Though Ubuntu 12.04 comes with Unity as the default you can switch back to Gnome, or choose other managers like Cinnamon.
If you want to know, which window manager you are running currently
echo $XDG_CURRENT_DESKTOP # tells you what window manager you are running
echo $GDMSESSION # tells you what option you've selected from login screen
find server ip address
To find a server IP address
ifconfig eth0 | grep inet | awk '{ print $2 }'
apache configuration file location
There is no default location for apache configuration file (httpd.conf) as this can be configured. To know the location run the below commands.
ps -ef | grep apache
root 4053 1 0 06:26 ? 00:00:04 /usr/sbin/apache2 -k start
www 5189 4053 0 11:00 ? 00:00:00 /usr/sbin/apache2 -k start
www 5199 4053 0 11:00 ? 00:00:00 /usr/sbin/apache2 -k start
Then simply run the below command to get the details.
/usr/sbin/apache2 -V
# Server compiled with....
# -D SERVER_CONFIG_FILE="/etc/apache2/a...
using ack in place of grep
To use Ack, make sure you have Perl installed and run the following commands in Ubuntu
sudo apt-get install ack
sudo apt-get install ack-grep
Now you can use ack-grep command to search for matches in the files
ack-grep -i some-text
# use ack-grep --help to list the possible options.
Check if package is installed in linux
To check if a given package is installed in Debian / Ubuntu, type
dpkg -s <package-name>
To get a neater output use dpkg-query which accepts wildcards as well.
dpkg-query -l <package-name>
In Redhat / Fedora / CentOS use the following command
rpm -qa | grep <package-name> # no output, package not installed
# to display list of all installed packages
rpm -qa
rpm -qa | less
Passing objects using redirect_to
Scenario : You have some manipulation done in your controller action and redirect the user to another action after that. Now you want the output of the manipulation available in the page user is redirected to.
You can pass the object with redirect_to but that will be added as a query parameter. redirect_to is always a GET request and the params are always visible to the user.
To achieve this you can either store the stuff in the session and clear it once you are done or pass the arbitrary objects to the template using flash. Though its a p...
understanding rake tasks
Rake is a build language (like make and ant), an internal DSL build in Ruby language.
task :task_name => [:clean] do
## ruby code comes here
## ruby code comes here
## ruby code comes here
end
task is a method that accepts two parameters, a hash and a block. The key in the hash is task name and the value is an array which contains the dependencies (optional). You can remove the square braces in case of single dependency and the value for the hash can be removed in case of no dependency.
The dependencies are executed first and...
detect and select method in ruby
detect method in ruby returns the first item in the collection for which the block returns TRUE and returns nil if it doesn't find any. It has an optional argument containing a proc that calculates a “default” value — that is, the value to return if no item in the collection matches the criteria without returning nil.
find is the alias for detect and find_all is an alias for select.
[1,2,3,4,5,6,7].detect { |x| x.between?(3,6) }
# 3
[1,2,3,4,5,6,7].select { |x| x.between?(3,6) }
# [3,4,5,6]
[1,2,3,4,5,6,7].find { |x| x.between?(13,61) ...
root user access in unix
To access root user privileges in Unix,
sudo su
## prompts for password and grants access
sudo su -
## switch as root user without password
listing methods of class and object (instance)
class Myclass
def one
puts "one is called"
end
end
ss = Myclass.new
puts Myclass.methods
# returns array of methods for the class. Includes both class and instance methods and methods inherited from parents.
puts Myclass.instance_methods
# returns array of instance methods for the class
puts ss.methods
# returns array of methods which can be called on the instance. same result as above.
puts Myclass.instance_methods(false)
# returns array of instance methods excluding inherited methods
To check if a method can be call...
include and extend in ruby
The extend method will mix a module’s methods at the class level.
On the other hand, the include method will mix a module’s methods at the instance level, meaning the methods will become instance methods of the class.
module Stringify
# Requires an instance variable @value
def stringify
if @value == 1
"One"
elsif @value == 2
"Two"
elsif @value == 3
"Three"
end
end
end
module Math
# Could be called as a class, static, method
def add(val_one, val_two)
BigInteger.new(val_one + val_two)
...
||= operator in Ruby
x ||= y is similar to x || x = y.
In the above statement, if x evaluates to logical true then assignment is not done.
If x evaluates to logical false only then the assignment is done.
Though the above two statements are similar, there are subtle differences.
# if x is not defined
puts x || x=y ## Undefined local variable or method `x`
puts x ||= y ## prints y
$: in Ruby
In Rails programming you might seldom come across the below statement
$:.unshift File.dirname(__FILE__)
$: represents load path in Ruby.
Its a pre-defined variable and short hand for $LOAD_PATH which ruby uses to look for files.
unshift prepends the given path to the beginning of the array (load path).
__FILE__
gives the relative path of the file from the current execution directory.
Incase, you need absolute path, use
File.expand_path(__FILE__)
NOTE : This explanation is as of Ruby 1.8.6. Latest versions of Ruby beh...
Change a string into class name
In Ruby, class names are constants, so you can do
str = "Thing" #=> "Thing"
Kernel.const_get(str) #=> Thing
Using send method in Ruby
In Ruby, send is often introduced as a way to show how everything is an object
1.send(:+, 1) ## -> 2
It can also be used to call private or protected methods from outside a given class as explained below.
class Myclass
def method_one
puts "instance method called."
end
private
def private_one
puts "private method called."
end
end
my_obj = Myclass.new
my_obj.method_one #=> "instance method called."
my_obj.private_one #=> NoMethodError: private method `private_one' called for #<Myclass:0xb739d9bc...
Fixing "uninitialized constant ActiveSupport::Dependencies::Mutex (NameError)"
If you have to work with older versions of Rails, there might be discrepancy between the versions of Rails and Rubygems. Theats when you likely see the below message when you try to boot your rails app.
/home/lt-pc-528/.rvm/gems/ruby-1.8.7-p371/gems/activesupport-2.3.7/lib/active_support/dependencies.rb:55:
uninitialized constant ActiveSupport::Dependencies::Mutex (NameError)
.....
......
You can fix the error by adding the following to environment.rb and script/server.rb files before you require boot.rb file.
require 'thread'...