Skip to content
Sergio Cambra edited this page May 22, 2024 · 6 revisions

Preparing

Make sure you are including jQuery.

Install

Add 'recordselect' to the Gemfile.

Then add the CSS and JavaScript to the application.js and application.css with require record_select (if you are using ActiveScaffold, it isn't needed, as the bridge will add them with require active_scaffold).

Enable and configure it on a controller. See available controller options.

class UsersController < ApplicationController
  record_select :per_page => 5, :search_on => 'username'
end
  1. Add the following to your routes.rb:
resources :model_id do
  record_select_routes
end
  1. Now go ahead and use it somewhere, with some helper in the view.

Advanced Usage

Using more options, such as searching on multiple columns, define the order, change the label, and display search mode toggle. Overriding record_select_model to change the query based on param, or in some permissions for the current user, which is set in the record_select_field helper in the view.

class UsersController < ApplicationController
    record_select search_on: [:username, :first_name, :last_name, :email],
      order_by: 'last_name ASC, first_name ASC',
      toggle_search_mode: true,
      label: proc { |u| "#{u.first_name} #{u.last_name} (#{u.username})" }


  protected

  def record_select_model
    logged_user = current_user(params[:ignore_impersonated].present?)
    
    if params[:action_actionee]
      User.has_assigned_actions.distinct
    elsif params[:action_assigned_by]
      User.has_delegated_actions.distinct
    elsif logged_user.can_impersonate_anyone?
      super      
    else
      User.managed_by(logged_user.username).or(User.where(id: logged_user))
    end
  end
end

Then use it in the view:

<%= record_select_field 'record[user]', selected_user || User.new, params: {action_actionee: 'true'} %>

Or in the controller if using ActiveScaffold:

class ActionsController < ApplicationController
  active_scaffold :"action" do |conf|
    conf.columns[:actionee].search_ui = :record_select, {params: {action_actionee: 'true'}}
  end

IF you are using a parameter in RS options, so RS filters the returned results, that param needs to be permitted in a helper, otherwise changing RS mode, typing or paginating won't keep the param. The super and concat is do address that you could be adding permitted params in multiple helpers, and do not want to overwrite.

module ActionsHelper
  def permit_rs_browse_params
    (super || []).concat [:action_actionee, :action_assigned_by]
  end