Ruby on Rails

Dynamic Scope Methods in Edge Rails

Posted in ror, Rails, Ruby on Rails on December 30th, 2008 by Satish Chauhan – Be the first to comment

We perform simple queries using dynamic finder and for commonly used query logic we use named scope.

but we can’t chain query conditions when using these dynamic finders.

With the addition of dynamic scopes, we can now have a way to both quickly specify query logic and chain further conditions.


We can now use class methods like scoped_by_user_name(user_name) and scoped_by_user_name_and_password(user_name, password) that will use the scoped method with attributes you supply.

Merb gets merged into Rails 3

Posted in Rails, Ruby on Rails on December 28th, 2008 by Satish Chauhan – Be the first to comment

Ruby on Rails and Merb team decided to  merge both of the frameworks into a singular web framework in Rails 3. The two frameworks have much in common and share code in a variety of places.

Please click here to find the detailed post by DHH.

First Ruby Fun Day : A Ruby And Ruby On Rails Event In India

Posted in Ruby, ror, Rails, Ruby on Rails on November 19th, 2008 by Satish Chauhan – Be the first to comment

Geekeerie organising a full day ruby and ruby on rails event scheduled on 22, Nov 2008 at Impetus Technologies, D-40, Sector 59,NOIDA,UP,India Noida, India.

And not just that, RubyFunDay comes with free goooodies like lunch and Ruby p T-Shirt.

So as you have signed up already for the event, please provide us your T-Shirt size here http://spreadsheets.google.com/viewform?key=p2AxAft0ZpLVXLO-mLbpPFw

For event details and participation confirmation: visit http://rubyonrails.in/events/3

How to load selective fixtures to db in rails

Posted in fixture, ror, Rails, Ruby on Rails on August 30th, 2007 by Satish Chauhan – 1 Comment

Al of you know how to load fixture to db

rake db:fixtures:load 

To load a subset of your fixtures to your projects db

rake db:fixtures:load FIXTURES=users,stores

Star rating in ruby on rails

Posted in rating, css, ajax, ror, Rails, Ruby on Rails on August 24th, 2007 by Satish Chauhan – Be the first to comment

Recently I needed to implement star rating system for one of my project I am working on.

If you are not familiar with a star rating system, its simply a method of voting using (usually) 5 stars in a row, which will change colour as you hover over them indicating the level at which to rate something. You may think a simple rollover would accomplish this but difficulty arises because as you rollover each star it should stay highlighted while you light up the next one and so on until the end of the row of stars.

If this doesnt make much sense to you then take a look at what we are going to achieve in this article. (I will leave the server-side coding to someone more knowledgeable and we will just concentrate on the visual aspects of making this effect happen. )

As you can see from the demo the stars stay hovered as you rollover each one. However, as mentioned earlier, the complicated part is that as each star is hovered it should stay alight while the next one is hovered so that if you start at the left side then 1,2,3,4 and 5 stars will be alight as you traverse the row. Usually a rollover will only be active on the anchor that the pointer is currently over making previous anchors lose their focus and thus their hover effect disappear.

 

Install the acts_as_rateable plugin.

Run the following from the root of your Rails app to install the plugin.


or
http://rubyforge.org/projects/rateableplugin/

Create the table(migration) to store rating into database

Model

models rateable

I was trying to add a rating system for the model Picture. Yours can obviously be whatever you like but from here on out I’ll be using Picture. So add acts_as_rateable to your model.

Controller to handle the rating

Views

Create the partial /views/rating/_rating.rhtml

A little RJS

Create the file /views/rating/rate.rjs

This will replace the star ratings with the partial we created previously in order to reflect any rating changes made by the submission.

Render the partial in one of your views.

This needs @picture(or whatver you’re going to be using) in order to function.

Remember to add the css and image

Image for your CSS

star_rating.gif


 

How to Paginate With Ajax

Posted in ajax, paginate, ror, Rails, Ruby on Rails on August 1st, 2007 by Satish Chauhan – Be the first to comment

Pagination is a very common task in web application development. But sometimes we need to paginate something with ajax request. The interest to use Ajax for this is to provide a dynamic interface which doesn’t need to reload the entire page when next results(page) displayed. Ajax is really nicely integrated with Rails, and using it is very easier with this great framework. To work with ajax requests your browser must be enabled to handled java script requests. I knew two methods for that problem.

First Method

Your Controller code

Layout

View

Second Method

Put this code in a helper somewhere

and use following code for creating links
View

How to use SyntaxHighlighter

Posted in SyntaxHighlighter, ror, Rails, Ruby on Rails on July 23rd, 2007 by Satish Chauhan – 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, ror, Rails, Ruby on Rails on July 18th, 2007 by Satish Chauhan – Be the first to comment

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