Advertisement
anso

Ransack search multiple words

Apr 16th, 2013
292
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rails 1.21 KB | None | 0 0
  1. # models/user.rb
  2.  
  3. class User < ActiveRecord::Base
  4.   has_secure_password
  5.   attr_accessible :email, :password, :password_confirmation, :profile_attributes, :group_ids
  6.  
  7.  
  8.   has_one :profile, :inverse_of => :user
  9.   accepts_nested_attributes_for :profile  
  10. end
  11.  
  12. # models/profile.rb
  13. class Profile < ActiveRecord::Base
  14.  attr_accessible :first_name, :last_name, :phone_fix, :phone_mobile, :user_id, :zip_code
  15.  belongs_to :user, :inverse_of => :profile
  16. end
  17.  
  18. # controllers/users_controller.rb
  19. class UsersController < ApplicationController
  20. def index
  21.     @q = User.joins(:profile).search(params[:q])
  22.  
  23.     @users = @q.result.paginate(:per_page =>10, :page => params[:page])
  24.  
  25.     respond_to do |format|
  26.       format.html # index.html.erb
  27.       format.json { render json: @users }
  28.     end
  29.   end
  30.  
  31.   def search
  32.     @q = User.joins(:profile).search(params[:q])
  33.     @users = @q.result.paginate(:per_page => 10, :page => params[:page])
  34.     render "index"
  35.   end
  36. end
  37.  
  38. # views/users/index.html.erb (search form only)
  39. <%= search_form_for @q, url: search_users_path, method: 'post', id: 'users-search-form' do |f| %>      
  40.         <%= f.text_field :profile_last_name_or_profile_first_name %>
  41.     <%= f.submit %>
  42. <% end %>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement