Showing posts with label rails3. Show all posts
Showing posts with label rails3. Show all posts

Tuesday, August 28, 2012

Add index on PostgreSQL table column for in-built full text search in Rails

Applicable for PG 8.4 and above.

class AddIndexesToListings < ActiveRecord::Migration
  def change
    execute <<-SQL
      create index on listings using gin(to_tsvector('english', title));
      create index on listings using gin(to_tsvector('english', description));
    SQL
  end
end

Configure PostgreSQL full-text search using pg_search gem for partial word match


class Listing < ActiveRecord::Base
  include PgSearch
  pg_search_scope :search_by_title_and_description, against: [:title, :description], using: { tsearch: { prefix: true } }
end

Thursday, August 16, 2012

Share methods between controller and views in Rails

The key is to put methods into ApplicationHelper and call them in the controller

module ApplicationHelper
  def user_unauthorized?
    ...
  end
end

class HomeController < ApplicationController
  def index
    redirect_to '/foo' if view_context.user_unauthorized?
  end
end

Tuesday, January 11, 2011

Rails arel/activerelation subquery gotcha



subsql = s.where(s[:name].matches("%#{site}%")).project(s[:id])

Incident.where(i[:supportable_type].eq('Site').and(i[:supportable_id].in([subsql]))).to_sql

=> "SELECT \"tickets\".* FROM \"tickets\" WHERE (\"tickets\".\"type\" = 'Incident') AND (\"tickets\".\"supportable_type\" = 'Site' AND \"tickets\".\"supportable_id\" IN (SELECT id FROM \"sites\" WHERE (\"sites\".\"name\" ILIKE '%Site A%')))"

Thursday, November 11, 2010

Error messages for Rails 3

# Deprecated

<%= error_messages_for :question %>

# Use partial in app/views/shared

<% if target.errors.any? %>  

<%= pluralize(target.errors.count, "error") %> prohibited this record from being saved:

    <% target.errors.full_messages.each do |msg| %>
  • <%= msg %>
  • <% end %>
<% end %> # Render partial in view <%= render "shared/error_messages", :target => @question %>

Thursday, March 4, 2010

Rails 3 notes

JQuery:

Grab the jQuery driver at http://github.com/rails/jquery-ujs and put it in your javascripts directory. Include the file in the layout.

http://joshhuckabee.com/jquery-rails-3

Forms:

Rails requires an authenticity token to do form posts back to the server. This helps protect your site against CSRF attacks. In order to handle this requirement the driver looks for two meta tags that must be defined in your page's head.

csrf_meta_tag
Routing:

Old

link_to :controller => 'user_sessions', :action => 'destroy'
New

# Route
match '/log' => "user_sessions#destroy", :as => :logout

# View
link_to 'Logout', '/logout'

Sunday, February 28, 2010

Rails, RedCloth, textilize gotcha


# This doesn't work! It returns html in pure strings and not the html elements!

textilize @blog.content

# Need this extra step.

sanitize(textilize @blog.content)


**UPDATED**

The problem lies that Rails 3 ERB uses h() helper by default

<\%= xxx %>
<\%= h(xxx) %>
To get past this use context_tag() or raw()

xxx = content_tag(:p, "blah")
<\%= xxx %>

xxx = "<\p>blah<\/p>"
<\%= raw xxx %>

Friday, February 5, 2010

Running Shoulda & Factory Girl in Rails 3

Rails 3 uses Bundler gem to handle gem dependencies. Encountered some problems with Shoulda and Factory Girl.

# Gemfile

group :test do
gem "shoulda"
gem "factory_girl"
end

Shoulda and Factory Girl macros are not loaded so I had to require the gems in test/test_helper.rb

# test/test_helper.rb

require 'shoulda'
require 'factory_girl'
Factory.definition_file_paths = [ File.join(Rails.root, 'test', 'factories') ]
Factory.find_definitions