How to use SyntaxHighlighter

Posted in Rails, Ruby on Rails, SyntaxHighlighter, ror on July 23rd, 2007 by admin – Be the first to comment

SyntaxHighlighter:

SyntaxHighlighter is a source code syntax highlighting plugin to help a developer/coder to post code snippets online with ease and and without having to worry about applying format. It is a free JavaScript tool for source code Syntex highlighting , It doesn’t care what you have on your server.

The idea behind SyntaxHighlighter is to allow insertion of colored code snippets on a web page without relying on any server side scripts.

WP-dp.SyntaxHighlighter supported languages:

  • C#
  • CSS
  • C++
  • VB & VB.NET
  • Delphi, Pascal
  • Java
  • JavaScript
  • PHP
  • Python
  • Ruby
  • SQL
  • XML, HTML, XSLT and any other XML style code

Click Here to download SyntaxHighlighter

http://syntaxhighlighter.googlecode.com/files/SyntaxHighlighter_1.5.0.zip

How to use SyntaxHighlighter

Place your code on the page and surround it with <pre> tag. Set name attribute to code and class attribute to one of the language aliases you wish to use.

<div class=”dp-highlighter> </div>

<pre name=”code” class=”ruby” style=”display: none;>
… some code here …
</pre>

An alternative to <pre> is to use <textarea> tag.

<textarea name=”code” class=”ruby”>
… some code here …
</textarea>

And You will get an output like

How to import CSV file in rails

Posted in CSV, Rails, Ruby on Rails, ror on July 18th, 2007 by Satish Chauhan – 2 Comments

How to import CSV file in rails?

The CSV text-file format is a common choice for both import and export when performing data migrations. What if you want to import CSV files within a Rails application?

Here is the code that allow you to upload CSV data onto the MySQL database from the web interface
This code save data direct to database without saving it to temp file

Controller:


View:
your view will look like

Verify Email address Format

Posted in Rails, Regexp, ror on July 3rd, 2007 by Satish Chauhan – 1 Comment

Useful expressions for email address validation

Matches a limited version of the RFC 2822 addr-spec form.

satish chauhan’s blog

Multiple Checkboxes with HABTM

Posted in CSV, HABTM, Rails, ror on June 29th, 2007 by Satish Chauhan – 5 Comments

Has and Belongs to Many with Multiple Check boxes

So if you are trying to do a multiple select of checkboxes and using habtm in your project, but when you submit the form, only one value was available in your controller. While you try to edit records in database but because of some error you get back to the pre field form and you found that the checkboxes checked by you gone ,then heres the solution

Model:

Controller Code:

Your View:

Helper method

View generate a checkbox for every interest(all_interest=Interest.find(:all)). The name of the input is significant obviously. The trailing [] on the name means the end result will be the list of checked ids. This list will be stored on the @params['customer'] hash with the key interest_ids. When the controller calls @customer.update_attributes(@params[:customer]), it tries to call @customer.key= for each of the keys on @params[:customer]. Whats important to realize is that these keys dont have to actually be attributes on the Customer model. All thats important is that theres a key= method on the model. Model automatically contains a collection_ids= method for habtm and has-many associations.

This method will load the objects identified by the ids and call the interest=(list) method on the model with the freshly loaded list. This method in turn, will compare the list to the current list of interests and delete/add interests as necessary. read more »

How to generate CSV files in Rails

Posted in CSV, Rails, ror on June 28th, 2007 by Satish Chauhan – 4 Comments

Controller code :

View
your view will look like