tag:blogger.com,1999:blog-43116133749610399222024-02-09T01:42:06.833+06:00Fuad's Blog on TechnologyAnonymoushttp://www.blogger.com/profile/11459190392904433194noreply@blogger.comBlogger13125tag:blogger.com,1999:blog-4311613374961039922.post-76887385860128494712015-08-17T15:48:00.001+06:002015-08-17T15:48:37.452+06:00My Travel CompanionsI am planning for a 2 weeks trip during the month of December in SAARC regions.<br />
<br />
I did some researches in the Internet and decided to take the following with me.<br />
<br />
1. <a href="http://www.amazon.co.uk/gp/product/B006P5PZM2?tag=fuadontech-21">Osprey Farpoint 40 Liter Backpack</a><br />
2. Two pants (Purchased from Grameen UNIQLO)<br />
3. Three shirts<br />
4. Two Lungi<br />
5. TwoT-shirts<br />
6. A warm Jacket<br />
7. <a href="http://www.amazon.co.uk/dp/B005DDVY72/?tag=fuadontech-21">A 700 ml water bottle</a><br />
8. A <a href="http://www.amazon.com/dp/B00GWE4PY0/?tag=fuadontech-21">Waterproof transparent bag</a><br />
9. A <a href="http://www.amazon.com/dp/B00CKRK0QK/?tag=fuadontech-21">sleeping bag</a><br />
10. A <a href="http://www.amazon.co.uk/gp/product/B00JRUFVE4?tag=fuadontech-21">lightweight towel</a><br />
11. A shorts<br />
12. A <a href="http://www.amazon.co.uk/gp/product/B00LIJ2DR0?tag=fuadontech-21">trekking Shoe</a><br />
13 A <a href="http://www.amazon.co.uk/gp/product/B00JGIXLSA?tag=fuadontech-21">Sandal</a><br />
14. Three pairs of socks and underwear<br />
15. A power bank<br />
16. Laptop<br />
17. Mobile<br />
<br />
What do you think about the list? Have I missed anything? Have I decided to take something unimportant?<br />
<br />
What do you usually take while going for a backpacking tour somewhere?<br />
<br />Anonymoushttp://www.blogger.com/profile/11459190392904433194noreply@blogger.com0tag:blogger.com,1999:blog-4311613374961039922.post-90916048646232088102011-06-17T20:22:00.000+06:002011-06-17T20:33:49.693+06:00Display Avatar in Ruby on Rails application<div dir="ltr" style="text-align: left;" trbidi="on">Now a days displaying Avatar is a common trend in web technology. Avatar is graphical representation of an user. Thanks go to Tom Preston Werner for introducing <a href="http://www.gravatar.com/">Gravatar</a> , a great web service which give globally recognized avatar. You just need to open up an account out their with an email address and upload your photo. Different websites, consuming Gravatar's service can display your photo if you register out there with the same email address.<br />
<br />
In this example I will show you a way to display Gravatar in your Ruby on Rails web application.<br />
<br />
First I will write a helper method which will return an image url for displaying Avatar. Gravater, id is simply the MD5 hexdigest of the email address. So the line returning avatar id will be:<br />
<pre class="brush :ruby">gravatar_id = Digest::MD5.hexdigest(user.email.downcase)
</pre><br />
I would like to display the avatar as 48x48, which is the usual size of a twitter display image. Also, I want to show a default image url if avatar is not shown. For these two, we need to pass some extra parameters. The method will look like:<br />
<br />
<pre class="brush :ruby">def avatar_url(user)
default_url = "#{root_url}images/default-image.jpg"
if(user.present?)
gravatar_id = Digest::MD5.hexdigest(user.email.downcase)
return "http://gravatar.com/avatar/#{gravatar_id}.png?s=48&d=#{CGI.escape(default_url)}"
else
return default_url
end
end
</pre>In your view:<br />
<br />
<pre class="brush :ruby"><%= link_to(image_tag(avatar_url(user)), user_path(user.id))%></pre><br />
The above line, will show the avatar and will take you to the show method of UsersController if you click on it.<br />
<br />
You can also choose from an user to display his avatar from gravatar or your local file system. All you need to do is to have a boolean field 'avatar' in your User table and re-arrange the logic in avatar_url method.<br />
<br />
It's that simple! Happy coding!!<br />
<br />
<br />
<br />
</div>Anonymoushttp://www.blogger.com/profile/11459190392904433194noreply@blogger.com0tag:blogger.com,1999:blog-4311613374961039922.post-35776900199806975632011-03-05T22:13:00.001+06:002011-06-17T22:35:12.102+06:00SEO friendly URL in ruby on rails and friendly_id<div dir="ltr" style="text-align: left;" trbidi="on"><div class="wlWriterEditableSmartContent" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:1413afc0-3584-4a03-98bf-49a61f5131b6" style="display: inline; float: none; margin: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">Technorati Tags: <a href="http://technorati.com/tags/SEO" rel="tag">SEO</a>,<a href="http://technorati.com/tags/Rails" rel="tag">Rails</a>,<a href="http://technorati.com/tags/SEO+in+Rails" rel="tag">SEO in Rails</a>,<a href="http://technorati.com/tags/SEO+friendly+URL+in+Rails" rel="tag">SEO friendly URL in Rails</a></div><br/><br />
By default Rails generate URL with ID. In short <span style="font-family: 'Courier New';">to_param</span> method returns a string, which Action Pack uses for constructing an URL to this object. The default implementation returns records’ <span style="font-family: 'Courier New';">id</span> as a string. <br />
Suppose, we have a <span style="font-family: 'Courier New';">Category</span> model. The <span style="font-family: 'Courier New';">routes.rb</span> will look like: <br />
<pre class="brush :ruby">map.resources :categories</pre>In categories.rb controller:<br />
<pre class="brush :ruby">@category = Category.find(1)</pre>In view <span style="font-family: 'Courier New';">index.html.erb</span>: <br />
<pre class="brush :ruby"><%=link_to(@category.title,category_path(@category))%></pre>This will generate the URL: <span style="font-family: 'Courier New';">/category/1</span><br />
<br />
While working on building a website <a href="http://www.khanacademybangla.com/">Khan Academy In Bangla</a> as a part of the translation project of <a href="http://www.khanacademy.org/">Khan Academy</a> , I realized that a SEO friendly URL is mandatory. In this website, I have categories like physics, biology or math and tutorials under each category. In order to make the user friendly URL I needed to overwrite to_param method in model.<br />
<br />
In order to make it SEO friendly, in Category.rb model I added the line:<br />
<pre class="brush :ruby">def to_param
"#{id}-#{title.downcase.gsub(/[^a-zA-Z0-9]+/, '-').gsub(/-{2,}/, '-').gsub(/^-|-$/, '')}"
end</pre>The regular expression will remove any special character. It generates the url: <span style="font-family: 'Courier New';">/category/1-biology</span>. Looks better, right? <br />
Although the URL has become SEO friendly now but this method has some drawbacks. To summarize some:<br />
<br />
1. It appends the id with the name. If we remove ID, then maintaining uniqueness will be a problem<br />
2. If the title field is edited then the URL will be changed causing SEO to break.<br />
3. For a large application, it will not be feasible to overwrite to_param in tons of model.<br />
<br />
In this point, I found a nice plugin/gem to make all these pretty simple and it is <a href="https://github.com/norman/friendly_id/">friedly_id</a> . It will do all the work for you with some simple steps. For my system what I did were:<br />
1. Install the gem: <br />
<pre class="brush :ruby">gem install friendly_id</pre>2. In environment.rb include the gem. <br />
<pre class="brush :ruby">require 'friendly_id'</pre>3. Generate friendly_id by going to my application directory: <br />
<pre class="brush :ruby">cd khanacademybangla
script/generate friendly_id</pre>4. Run the migration <br />
<pre class="brush :ruby">rake db:migrate</pre>5. Add has_friendly_id to the models which I am looking for to generate user friendly id <br />
<pre class="brush :ruby">class Category < ActiveRecord::Base
has_friendly_id :title, :use_slug => true
end</pre>6. Run a rake task to generate slugs for existing records: <br />
<pre class="brush :ruby">rake friendly_id:make_slugs MODEL=ModelName</pre>For my example it was:<br />
<pre class="brush :ruby">rake friendly_id:make_slugs MODEL=category</pre>That’s it! Now we will be able to see a completely SEO friendly url: <span style="font-family: 'Courier New';">/category/biology</span><br />
<br />
I am yet to push my code to production! You need to wait till tomorrow to see it live.</div>Anonymoushttp://www.blogger.com/profile/11459190392904433194noreply@blogger.com5tag:blogger.com,1999:blog-4311613374961039922.post-28807500960459185052010-01-13T10:17:00.001+06:002010-01-13T10:45:10.249+06:00Displaying status feed of twitter in Rails<p><div class="wlWriterEditableSmartContent" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:ca366b6c-06c2-4ca1-b91b-99b4d74f942c" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">Technorati Tags: <a href="http://technorati.com/tags/Ruby+on+Rails" rel="tag">Ruby on Rails</a>,<a href="http://technorati.com/tags/Twitter+feed+in+rails" rel="tag">Twitter feed in rails</a>,<a href="http://technorati.com/tags/twitter+gem" rel="tag">twitter gem</a>,<a href="http://technorati.com/tags/HTTP+Auth+in+twitter" rel="tag">HTTP Auth in twitter</a></div></p><p>Couple of days ago I needed to show twitter status in a rails application. The specification was to show the last 5 status of a twitter account. Twitter itself has a good API. But I used twitter gem which left me to do just a couple of lines of code. </p><p>Twitter gem supports both OAuth and HTTP Auth. I used HTTP Auth for authentication. Let me dig in to code.</p><p>First of all, install <a href="http://twitter.rubyforge.org/rdoc/" target="_blank">twitter gem.</a></p><pre class="brush :ruby">gem install twitter</pre>Load ruby gem to environment.rb.<br />
<pre class="brush :ruby">require 'twitter'</pre><p>Then perform the authentication using HTTP Auth. You need to provide your user id and password of the twitter account. You should read this from a config file. For the simplicity I am placing it at code.</p><pre class="brush :ruby">def twitter_feed
httpauth = Twitter::HTTPAuth.new(‘twitter_account_name’, ‘twitter_account_password’)
twitter_client = Twitter::Base.new(httpauth)
@latest_status_feeds = twitter_client.user_timeline[0..4] if twitter_client.present?
#it will show most recent 5 status. If you would like to show recent 20 don't put any bar.
render :partial=>"twitter_feed"
end
</pre><p>In twitter_feed partial you can display the text using some properties.</p><pre class="brush :ruby"><h4>Latest Tweet</h4>
<%if @latest_status_feeds.present?%>
  <%  @latest_status_feeds.each do | @latest_status_feed|%>
    <p>"<%= @latest_status_feed.text %>"<br /><span class="time" style="font-size:11px;font-style:italic;">
<%= convert_date(@latest_status_feed.created_at.to_date) %></span></p>
   
  <%end%>
<%end%>
</pre><p>This will show the last 5 status from your twitter feed.</p><p>If you would like to show the twitter feeds of your friends, the following tweak is needed to be made:</p><pre class="brush :ruby">    @friends_feeds =     twitter_client.friends_timeline </pre><p>You can display from the view in the same way! </p><p>That's it! Pretty simple, yeah? Happy coding.</P>Anonymoushttp://www.blogger.com/profile/11459190392904433194noreply@blogger.com0tag:blogger.com,1999:blog-4311613374961039922.post-34750589463014821582009-12-31T17:57:00.001+07:002009-12-31T19:32:15.535+07:00Presentation on bad smells in code<p>Most of you must have read the famous book on “Refactoring” written by Martin Fowler. Based on this book and and my fellow colleague Rashed Kibria presented at the BASIS, titled as “Bad Smells in code (Part-1)”. We have shared the document. You can take a look at it.</p><a title="View Bad Smell in Codes-1 on Scribd" href="http://www.scribd.com/doc/24112859/Bad-Smell-in-Codes-1" style="margin: 12px auto 6px auto; font-family: Helvetica,Arial,Sans-serif; font-style: normal; font-variant: normal; font-weight: normal; font-size: 14px; line-height: normal; font-size-adjust: none; font-stretch: normal; -x-system-font: none; display: block; text-decoration: underline;">Bad Smell in Codes-1</a> <object codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" id="doc_267779635668753" name="doc_267779635668753" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" align="middle" height="500" width="450" > <param name="movie" value="http://d1.scribdassets.com/ScribdViewer.swf?document_id=24112859&access_key=key-2ayzc3yavv1y9w4magip&page=1&version=1&viewMode=slideshow"><param name="quality" value="high"><param name="play" value="true"><param name="loop" value="true"><param name="scale" value="showall"><param name="wmode" value="opaque"><param name="devicefont" value="false"><param name="bgcolor" value="#ffffff"><param name="menu" value="true"><param name="allowFullScreen" value="true"><param name="allowScriptAccess" value="always"><param name="salign" value=""><param name="mode" value="slideshow"><embed src="http://d1.scribdassets.com/ScribdViewer.swf?document_id=24112859&access_key=key-2ayzc3yavv1y9w4magip&page=1&version=1&viewMode=slideshow" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" play="true" loop="true" scale="showall" wmode="opaque" devicefont="false" bgcolor="#ffffff" name="doc_267779635668753_object" menu="true" allowfullscreen="true" allowscriptaccess="always" salign="" type="application/x-shockwave-flash" align="middle" mode="slideshow" height="500" width="450"></embed> </object>Anonymoushttp://www.blogger.com/profile/11459190392904433194noreply@blogger.com2tag:blogger.com,1999:blog-4311613374961039922.post-48075920327588809692009-12-30T22:31:00.001+07:002009-12-31T10:59:02.657+07:00Formatting date in Rails<p>The thing with which we deal everyday in our programming life is “Date”. This small, delicate stuff needs to be displayed in different formats in different places! In Rails there are many useful helpers to format date. Hence I am jolting down some so that I can take look quickly if I need one of them to use. You can take a look at it, too.</p><p>I have used <strong>strftime</strong> function of <strong>ActiveSupport</strong> class. </p><pre class="brush :ruby">current_time = Time.now #today is 30 December, 2009
current_time.strftime("%m-%d-%y") # "12-30-09"
current_time.strftime("%b %d, %Y %I:%M%p") # "Dec 30, 2009 10:23PM"
current_time.strftime("%A %d, %B %Y") # "Wednesday 30, December 2009"
current_time.strftime("%A, %d %B %Y %I:%M%p") # "Wednesday, 30 December 2009 10:33PM"
current_time.strftime("%Y-%m-%d %H:%M:%S") # "2009-12-30 22:33:55"
current_time.strftime("%b %d, %Y") #=> "Dec 30, 2009"</pre><br />
<br />
By this time you may have understood the meaning of this format. Here is the explanation:<br />
<br />
%Y – 2009 – Year with century<br />
<br />
%y – 09 – Year without century<br />
<br />
%B – January – Full month name<br />
<br />
%b – Jan – Short month name<br />
<br />
%m – 12 – Month number<br />
<br />
%A- Wednesday-Full weekday name<br />
<br />
%a- Wed-Short weekday name<br />
<br />
%H –22 – Hour<br />
<br />
%M –33 – Minute<br />
<br />
%S –55 - Second<br />
<br />
%p – AM or PMAnonymoushttp://www.blogger.com/profile/11459190392904433194noreply@blogger.com1tag:blogger.com,1999:blog-4311613374961039922.post-86467625927293714142009-12-29T18:05:00.001+07:002011-03-05T23:39:31.997+06:00Some useful number helpers in rails<div dir="ltr" style="text-align: left;" trbidi="on">In my previous blog on Rails I discussed about some useful text helpers for rails based on my work experience on <a href="http://www.scrumpad.com/companies/new?referrer=FO-00010">Scrumpad</a>. Now I would like to shade on some useful number helpers in rails which made my life easy during the project work. Let’s dive into some samples!<br />
<strong><br />
</strong><br />
<strong>1. number_to_currency</strong><br />
Software developers do the complex thing and forget about small ones. Sometimes after design discussions, writing a lot of lines of codes with complex logic create a number. Say, bill of a particular company for the month of January! When you will deliver the result to your product owner with pride, he will take a look at the number (you are expecting a pat on your back ) and ignoring your days of work may start talking with an unsatisfied face that “You forgot the currency. Is it in pound? I will be damn rich, then”. Now you come down to the earth! What a silly thing it was using number helpers in rails!! These methods are from ActionView::Helpers::NumberHelper <br />
<pre class="brush :ruby">number_to_currency(1234.506) # $1234.51
number_to_currency(1234.506, :precision=>3) # $1234.506
number_to_currency(1234.506, :precision=>3, :unit=>”&pound”) # &pound1234.506
</pre><br />
<strong>2. number_to_human_size</strong><br />
A lot of systems deal with user interaction now a days. Attachment is an important part of it. Users like to upload files like images, texts, documents and so on. Beside a beautiful icon of that attachment, it is nice to mention the size of that file. It can be easily done by rails using this function. <br />
<br />
<pre class="brush :ruby">number_to_human_size(123) # 123 Bytes
number_to_human_size(1234) # 123 KB
number_to_human_size(1234567) # 1.2 MB</pre><br />
It can be used with precision and separator, too.<br />
<br />
<pre class="brush :ruby">number_to_human_size(1234567, :precision=>2, :separator=>’,’) # 1,18 MB</pre><br />
<strong>3. number_to_percentage</strong><br />
It formats a number as percentage strings. At some places discount is calculated dynamically based on number of active users. Here comes a useful helper.<br />
<br />
<pre class="brush :ruby">number_to_percentage(100) #100%</pre><br />
<strong>4. number_to_phone</strong><br />
It formats a phone number to US phone number format. Once can customize it, too. It takes area code, country code, extension as option hash.<br />
<br />
Let’s give some example:<br />
<br />
<pre class="brush :ruby">number_to_phone(1115678) # 111-5678
number_to_phone(1114567890, :area_code=>true) # (111) 456-7890
number_to_phone(1114567890, :area_code=>true, :extension=>999) # (111) 456-7890 x 999
number_to_phone(1114567890, :country_code=>1 # +1- 111 - 456-7890
</pre><br />
So, based upon your requirement you can show the user phone number.<br />
<br />
That’s it for today. I will come shortly with more helper that I usually use! Happy coding :-)</div>Anonymoushttp://www.blogger.com/profile/11459190392904433194noreply@blogger.com0tag:blogger.com,1999:blog-4311613374961039922.post-49433836465560304032009-12-24T21:49:00.001+07:002009-12-29T18:22:01.110+07:00Some useful text helpers in Rails (Part - 1)<div class="wlWriterEditableSmartContent" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:bf6f3c5b-9c24-4bf2-89df-23e150c82ab6" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">Technorati Tags: <a href="http://technorati.com/tags/Ruby+on+Rails" rel="tag">Ruby on Rails</a>,<a href="http://technorati.com/tags/ActionView%3a%3aHelpers%3a%3aTextHelpers" rel="tag">ActionView::Helpers::TextHelpers</a>,<a href="http://technorati.com/tags/text+helpers+in+rails" rel="tag">text helpers in rails</a></div><p>One of the most important features of Rails is it’s rich helper. Sometimes these are life savers because you don’t have to write your own code and very useful if you can use it in a clever way. While developing scrumpad ( <a href="http://www.scrumpad.com">scrumpad</a> )  I found many places where texts need to be formatted and Rail’s helper came like a great boon. Let’s share some helpers!</p><p><strong>1.  auto_link</strong></p><p>Suppose your text contains both url and email. auto_link will convert it to as links.</p><pre class="brush :ruby">auto_link(“Go to www.scrumpad.com and meet fuad@code71.com”)</pre>It will place links automatically at the appropriate places. If you want to make the sites as url but not the mail, you can pass the option too like:</p><pre class="brush :ruby">auto_link(“Go to www.scrumpad.com and meet fuad@code71.com” , :link=>:urls)</p><p>auto_link(“Go to www.scrumpad.com and meet fuad@code71.com” , :link=>:email_address)</pre><p> </p><p><strong>2. cycle</strong></p><p>While providing alternate colors for tables for style purpose, we often put logic in the code to determine the even and odd row and places the color alternatively. This can be handled by a nice helper named “cycle”</p>suppose you have two css classes named “even” and “odd”. You can make the alternate rows to alternate colors by:<br />
<pre class="brush :ruby"><tr class=”<%= cycle(“even”, “odd”)>”<td>Data</td> </tr>
</pre><br />
<p><strong>3. word_wrap</strong></p><p>Yes! You can guess it by it’s name. If you want to wrap a long line with (or without) a specified width you just call this helper.</p><pre class="brush :ruby">word_wrap(“A brown colored fox jumped over the lazy dog”, :line_width=>5)</pre><p><strong>4.  truncate</strong></p><p>At times we wish to show the users a part of our long text and replace the lest with “…”. Rails makes it easy for us as usual.</p><pre class="brush :ruby">truncate(“Scrumpad, a agile project management tools by Code71”, 19) </pre><p>Output will be something like  “Scrumpad, an agile…”</p><p>If you want to provide your text instead of “…” you should write it as:</p><pre class="brush :ruby">truncate(“Scrumpad, a agile project management tools by Code71”, 19, “….(More)”) </pre><p>Output will be like “Scrumpad, an agile…(More)”</p><p>That’s all from today. I will come back to you guys with more. In the meantime you can try these out (If you haven’t tried these yet :-) )</p>Anonymoushttp://www.blogger.com/profile/11459190392904433194noreply@blogger.com1tag:blogger.com,1999:blog-4311613374961039922.post-3117343789253117362009-08-19T19:11:00.001+07:002009-08-19T19:15:49.186+07:00Showing success/failure message for CRUD in railsHow do you know whether a CRUD is successful in rails?How do you show an user friendly error message based on different error? Here is a brief description: <br /><br /><pre class="brush :ruby"><br />#create <br /><br />def create <br />@is_success = false <br />@build = Build.new(params[:build]) <br />begin <br />@build.save! <br />@is_success = true <br />rescue Exception=>ex <br />@message = "Build cannot be saved because #{ex.message} " <br />end <br />end <br /></pre><br /><br />Now from view based on the value of @is_success you can deliver user friendly message. What in case of update? The code will look somehow different. :-) <br /><pre class="brush :ruby"><br />def update <br />@build = Build.find(params[:id]) <br />@is_success = @build.update_attributes!(params[:build]) <br />end<br /></pre><br /> <br />Now comes destroy!<br />If the destroy() is succesful then frozen method will return true! That is how you can be sure whether an object is deleted! <br /><br /><pre class="brush :ruby"><br />def destroy <br />@is_success = false <br />@build = Build.find(params[:id]) <br />@build.destroy() <br />@is_success = @build.frozen? <br />end <br /></pre><br />That's it! Simple!! Happy coding :-)Anonymoushttp://www.blogger.com/profile/11459190392904433194noreply@blogger.com0tag:blogger.com,1999:blog-4311613374961039922.post-22744717995094665002009-01-12T10:50:00.001+06:002009-01-12T12:01:06.359+06:00Using subversion client with Net Beans 6.5 and Tortoise 1.5.x<p>I recently upgraded my Net Beans to it’s 6.5 version and Tortoise to 1.5.x. Since then my subversion client is not working and showing an error message as “please get a newer Subversion client”. The fix is the following one:</p> <p>Go to Tools=>Plugins=>Available Plugins and install Subvesion 1.5 for windows bridge.</p> <p>If it does not fix your problem then,</p> <p>Go to <em>Tools=> Options=> Miscellaneous=> Versioning=> Subversion</em>. You will find an input field to specify<em> SVN home folder.</em> You can specify where you installed your latest Subversion client software here</p> <div class="wlWriterEditableSmartContent" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:420d61c5-1741-4b72-a905-8c1f278f5fd9" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">Technorati Tags: <a href="http://technorati.com/tags/net+beans+6.5" rel="tag">net beans 6.5</a>,<a href="http://technorati.com/tags/svn+client" rel="tag">svn client</a></div> Anonymoushttp://www.blogger.com/profile/11459190392904433194noreply@blogger.com1tag:blogger.com,1999:blog-4311613374961039922.post-29873890657400800442009-01-10T20:13:00.001+06:002009-08-21T21:44:20.602+07:00Setting user defined time zone in Rails 2.1<p>Time zone support in rails is now easier than ever. You can set your time zone dynamically based on users information.</p> <p>Here are the steps:</p> <br />In configuration file, config/environment.rb add the following line<br /><pre class="brush :ruby">config.time_zone = "UTC"</pre><br />You can prompt from user for their time zone and save it in the database for future reference. For the population of time zones in the dropdown list add the following line in you *.rhtml file:<br /><pre class="brush :ruby"><br /><%= time_zone_select "user", "time_zone", TimeZone.all%><br /></pre><br />Save this data in the database field.<br />When the user will be logged in fetch the time_zone from database and set it at Time.zone.<br /><pre class="brush :ruby">def set_timezone(time_zone)<br /> Time.zone = time_zone<br />end</pre><br />To persist it every where make sure to call it:<br /><pre class="brush :ruby">set_timezone(@logged_in_user.time_zone)</pre><br /><p>That’s it!</p>Anonymoushttp://www.blogger.com/profile/11459190392904433194noreply@blogger.com0tag:blogger.com,1999:blog-4311613374961039922.post-81003524050445908532008-12-03T12:01:00.000+06:002009-12-31T11:05:59.388+07:00Redirecting a http request to https in Rails<p>In my previous blog i instructed how to install a SSL certificate in server. In this tutorial I will provide you some insights on how to redirect a http request to a https.<br />
</p><p>we need two methods:<br />
</p><br />
1. One is for going to from http to https:<br />
<pre class="brush :ruby">def require_https
redirect_to :protocol => "<a href="https://%22/">https://"</a> unless (request.ssl? or local_request? or request.post? or ENV["RAILS_ENV"]=="development")
end
</pre><br />
2. The other is the reverse of this one, that is going from https to http:</p><pre class="brush :ruby">def require_http
redirect_to :protocol => "<a href="http://%22/">http://"</a> if (request.ssl?)
end</pre><br />
<br />
Now you can call these two function where it is necessary like:<br />
<pre class="brush :ruby">before_filter :require_https</pre><br />
<br />
In this point you may get an error like “Infinite Redirection loop”. In order to solve this problem add the following line at<br />
<pre class="brush :ruby">RequestHeader set X_FORWARDED_PROTO 'https'
</pre><br />
So the config file should be something like this:<br />
<br />
<pre class="brush :ruby"><VirtualHost *:443>
RequestHeader set X_FORWARDED_PROTO 'https'
SSLEngine on
SSLCertificateFile /etc/apache2/SSL_Files/abc.crt
SSLCertificateKeyFile /etc/apache2/SSL_Files/abc.key
SSLCertificateChainFile /etc/apache2/SSL_Files/gd_bundle.crt
</VirtualHost>
</pre><p>Now any request coming to http should be redirected to https.</p><p>Hope this will serve your purpose. Happy coding!! </p>Anonymoushttp://www.blogger.com/profile/11459190392904433194noreply@blogger.com0tag:blogger.com,1999:blog-4311613374961039922.post-66441942796813157792008-12-01T18:35:00.000+06:002009-01-11T13:32:47.426+06:00Steps for installing SSL certificate in Linux<p>Here is a complete process of installing SSL certificate in Linux box which run in Amazon EC2 instance and served by Apache. I will also provide suggestion to show how homepage can be served in https while invoking the default page.</p> <p>I tested with:</p> <p>1. Linux version 2.6.16 (Red Hat 4.0.1-5)</p> <p>2. Apache 2</p> <p>You will need an installed copy of OpenSSL in the box. It is installed by default with Apache2. If you do not have it installed you should install it from <a title="http://www.openssl.org/" href="http://www.openssl.org/">http://www.openssl.org/</a></p> <p>So here are the steps:</p> <p>1. Generating a private key:</p> <p>To create a private key Go to /etc/apache2/conf.d and use the following command:</p> <p>/usr/bin/openssl genrsa –rand /dev/urandom –out /etc/apache2/conf.d/server.key</p> <p>Hence a private key file named “server.key” will be generated at the /etc/apache2/conf.d</p> <p>2. Generating CSR (certificate signing request):</p> <p>For creating a file named server.csr at the same directory the following command should be written:</p> <p>/usr/bin/openssl req –new –key /etc/apache2/conf.d/server.key –out /etc/apache2/conf.d/server.csr</p> <p>After proving this command some information should be provided. A CSR file will be generated after this one.</p> <p>3. Creating a Certificate: </p> <p>You can generate a Commercially Signed Certificate file from any Commercially Signed Authority by providing the private key and csr.</p> <p>4. Installing the certificate:</p> <p>Go to /etc/apache2/sites-available/default and add the following configuration:</p> <p><VirtualHost *:443> <br /> SSLEngine on <br /> SSLCertificateFile /etc/apache2/SSL_Files/abc.crt <br /> SSLCertificateKeyFile /etc/apache2/SSL_Files/abc.key <br /> SSLCertificateChainFile /etc/apache2/SSL_Files/gd_bundle.crt <br /></VirtualHost></p> <p>Make sure that port 443 is open.</p> <p>That’s it!!Now your certificate is installed in the server.</p>Anonymoushttp://www.blogger.com/profile/11459190392904433194noreply@blogger.com1