Rails Solr Sunspot Geospatial Searches
Using Gem “sunspot_solr” 1.3.0 I have “lat” and “lng” attributes associated with my model. I defined searchable as: model.rb searchable do float :lat, :as => ‘lat’ float :lng, :as => ‘lng’ end controller.rb def search search = Model.search do # 25 miles hard coded for now search_radius = 25 adjust_solr_params do |params| params[:spatial]="{!radius=#{search_radius} sort=true}lat:#{current_user.lat},lng:#{current_user.lng}" end [...]
ActiveRecord callbacks and destructive methods
Recently I added search functionality to my application. I implemented a search controller and model. An issue I faced was manipulating the search keywords entered by the user so the search query worked properly. I ended up doing the following: search.rb class Search < ActiveRecord::Base belongs_to :user attr_accessible :keywords, :category, :user validates_presence_of :keywords [...]
Ruby on Rails 3 RESTful and default routes
You have probably created some action in your controller class and can’t access it right? Well you need to have some default routing rules set up in order for rails to understand the url and route it to the appropriate action and controller. This will happen alot with AJAX calls where you simply want to [...]
Example using ruby blocks, lambda and Proc
What really distinguishes ruby from other languages is the syntatic sugar of blocks. For example, blocks really come in handy when iterating thru a collection. The example: (1..10).collect{|i| i*i} => [1, 4, 9, 16, 25, 36, 49, 64, 81, 100] But how does the collect method work? Let’s define an array containing all the prime [...]
Dynamically update URL using jQuery
I am using jQuery FullCalendar for a project and the view can be either a day, a month, or a year. The user can move around the calendar by clicking back and forward arrows, and none of these clicks will cause a page refresh. However the “start” and “end” dates change depending on the current [...]
How to install RVM with ruby on rails
Learn how to install and configure RVM with both rails 3 and rails 2 apps. RVM allows users to deploy each project with its own self-contained and dedicated environment–from the specific version of ruby all the way down to the precise set of required gems to run the application.
Ruby to Javascript Dates
Recently I was using the JQuery UI datepicker widget and needed to restrict dates to a range in the popup calendar selector. In order to do so I had to pass the Ruby Date to javascript. My goal is usually to write as little javascript/jquery code as possible, i.e. to do all the necessary conversions [...]