• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Ransack ruby on rails - HuanND
 

Ransack ruby on rails - HuanND

on

  • 1,473 views

 

Statistics

Views

Total Views
1,473
Views on SlideShare
1,473
Embed Views
0

Actions

Likes
0
Downloads
2
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Ransack ruby on rails - HuanND Ransack ruby on rails - HuanND Presentation Transcript

    • Ruby on railsRanSackDesign by Mr.Huân
    • Whats RansackRansack is a rewrite of MetaSearch. While it supportsmany of the same features as MetaSearch, its underlyingimplementation differs greatly from MetaSearch, andbackwards compatibility is not a design goal.Ransack enables the creation of both simple andadvanced search forms against your applications models.
    • Basic Searching• Eq :The eq predicate returns all records where a fieldis exactly equal to a given value.>> Product.search(:name_eq => "Flux Capacitor").result.to_sql=> "SELECT "products".* FROM "products" WHERE "products"."name" = FluxCapacitor"• Matches• lt• In• Cont• Start• End
    • Usage ransackRansack can be used in one of two modes:•Simple Mode• The default param key for search params is now :q, insteadof :search.• form_for is now search_form_for and validates that aRansack::Search object is passed to it.• Common ActiveRecord::Relation methods are no longerdelegated by the search object. Instead, you will get your searchresults via a call to Search#result.
    • Simple Mode(continue)In your controller:def index@q = Person.search(params[:q])@people = @q.result(:distinct => true)endIn your view:<%= search_form_for @q do |f| %><%= f.label :name_cont %> <%= f.text_field :name_cont %><%= f.label :articles_title_start %> <%= f.text_field :articles_title_start %><%= f.submit %><% end %>//View list Products
    • Advanced Search• "Advanced" searches (ab)use Rails nested attributes functionality inorder to generate complex queries with nested AND/OR groupings,etc. A notable drawback with these searches is that the increasedsize of the parameter string will typically force you to use the HTTPPOST method instead of GET.• routes...resources :products docollection { post :search, to: products#index }endend
    • Advanced Search(Continue)•  controller action ...def search index render :index end• search_form_for line in the view ...<%= search_form_for @q, :url => search_products_path, :html => {:method => :post} do |f| %>
    • has_many and belongs_toassociationsCan easily use Ransack to search in associated objects.Model:class Product < ActiveRecord::Base     belongs_to :Categoryend class Category < ActiveRecord::Base      has_many :Productsend 
    • class ProductsController < ApplicationController def index         @search = Product.search(params[:q]    @Products = @search.result(:distinct => true) end endForm…<%= search_form_for @search do |f| %>   <%= f.label :name_cont %> <%= f.text_field :name_cont %>   <%= f.label :category_name_cont %> <%= f.text_field :categoris_last_name_cont %>   <%= f.submit "search" %> <% end %>
    • Thank for watching