






<!-- categories/index.js.erb --> <%= auto_complete_values @categories, :name, :id %><%= text_field_with_auto_complete :product, :category_name, { :size => 15 }, { :url => categories_path(:js), :method => :get, :param_name => 'search', :after_update_element => "function(text_field, li_element){$('product_category_id').value = $('text_' + li_element.id).value;}"}) %> <%= hidden_field "product", "category_id" %><!-- application_helper --> def auto_complete_values(entries, text_field, value_field, phrase = nil) return unless entries items = entries.map do |entry| content_tag("li", phrase ? highlight(entry[text_field], phrase) : h(entry[text_field]), {:id => entry[value_field]}) + hidden_field_tag("text_#{entry[value_field]}", entry[value_field]) end content_tag("ul", items.uniq, {:style=> "height: 150px; overflow:auto;"}) end
Note: In case you want to trigger an event on select of autocomplete list then all you want to do is modify after_update_element
Ex: <%= text_field_with_auto_complete :company, :number, {:class => "title"}, {:url => autocomplete_company_numbers_path(:js), :method => :get, :param_name => 'search', :after_update_element => "function(text_field, li_element){window.location.href='/company__numbers/show/' + $('text_' + li_element.id).value;}"} %>
$ gem install jsonThen, create your Rails project
$ rails couchifyThen, install ActiveCouch as a plugin:
$ cd couchify
$ ./script/plugin install git://github.com/arunthampi/activecouch.git
This installs ActiveCouch in RAILS_ROOT/vendor/plugins
development:
site: http://localhost:5984/
test:
site: http://localhost:5984/
production:
site: http://localhost:5984/
class Cat < ActiveCouch::Base
site YAML::load(File.open(File.join(Rails.root,
'config', 'activecouch.yml')))[Rails.env]['site']
endAll set to use CouchDB database depending on your Rails environment. Now ActiveCouch assumes that you already have a database called ‘cats’ created in CouchDB, so that you can start creating all your CouchDB documents. No matter, Rake tasks to the rescue:
$ rake --tasksYou will see some of the following:
rake activecouch:create_db # Creates a database in CouchDB
rake activecouch:delete_db # Deletes a database from CouchDB
rake activecouch:delete_view # Deletes a view in CouchDB
rake activecouch:save_view # Saves a view in CouchDBSo to create your database, just enter
$ rake activecouch:create_db db=catsAnd boom, you have your database called cats created in ActiveCouch.
$ rake activecouch:delete_db db=catsAwesome, now you can go ahead and edit your model to have various fields, for example a cat should probably have a name, so your model will look something like this:
class Cat < ActiveCouch::Base
site YAML::load(File.open(File.join(Rails.root,
'config', 'activecouch.yml')))[Rails.env]['site']
has :first_name
has :last_name
end
> cat = Cat.new(:first_name => "Mr.", :last_name => "Jinks")
> cat.saveOr
> cat = Cat.create(:first_name => "Mrs.", :last_name => "Jinks")
cats = Cat.find(:all, :params => {:last_name => "Jinks"})The price you pay for the somewhat unwieldy view names, is that you get very convenient find methods. Of course if you have created your custom views, you can always use the find_from_url method which works something like this:
cats = Cat.find_from_url("/cats/_view/by_last_name/by_last_name?key=%22Jinks%22")Ok, back to views. ActiveCouch lets you create your views using a very simple generator. So from your RAILS_ROOT again,
$ ./script/generate activecouch_view ByLastNameThis creates an ActiveCouch::View class in RAILS_ROOT/app/models. You can edit the view as given below, so that you can start querying by last name:
class ByLastName < ActiveCouch::View
define :for_db => 'cats' do
with_key 'last_name'
end
end
Done. But this view is not yet in the CouchDB database, so you need to save it into the database.
$ rake activecouch:save_view db=cats view=ByLastNameAnd boom, your view is saved.
$ rake activecouch:save_view db=_all_dbs view=ByLastNameNow you are ready to start querying your database and finding objects.http://127.0.0.1:5984/_utils For more information refer the links:http://wiki.apache.org/couchdb/Installing_on_Windows <%= link_to "Printable Invoice (PDF)", order_path(@order, :format => 'pdf') %>prawnto :prawn => { :top_margin => 75 } def show @order = Order.find(params[:id]) end
create show.pdf.prawn in your views and add pdf.text "Order ##{@order.id}", :size => 30, :style => :bold pdf.move_down(30) items = @order.cart.line_items.map do |item| [ item.product.name, item.quantity, number_to_currency(item.unit_price), number_to_currency(item.full_price) ] end pdf.table items, :border_style => :grid, :row_colors => ["FFFFFF","DDDDDD"], :headers => ["Product", "Qty", "Unit Price", "Full Price"], :align => { 0 => :left, 1 => :right, 2 => :right, 3 => :right } pdf.move_down(10) pdf.text "Total Price: #{number_to_currency(@order.cart.total_price)}", :size => 16, :style => :bold
For more information refer: http://railscasts.com/episodes/153-pdfs-with-prawnFor more information on prawnto refer: http://www.cracklabs.com/prawnto
| No regions selected | ||
|---|---|---|
| State | Super Region | Region |
| <%= state_name %> | <%if region.super_region.nil? super_region_name = "" else super_region_name = region.super_region.super_region_description end%><%= super_region_name %> | <%if region.region.nil? region_name = "" else region_name = region.region.region_description end%><%= region_name %> |
* ActiveRecord models * Fixture files * Tests and Specs * Object Daddy exemplars * Machinist blueprintsThe schema comment looks like this:
# == Schema Info
#
# Table name: line_items
#
# id :integer(11) not null, primary key
# quantity :integer(11) not null
# product_id :integer(11) not null
# unit_price :float
# order_id :integer(11)
#
class LineItem < ActiveRecord::Base
belongs_to :product
. . .