Created: 2014-05-18 18:02
Updated: 2018-05-11 18:51
License: mit

Code Climate Test Coverage Build Status


SimpleForm and Ransack combined.


In your Gemfile:

gem 'simple_form_ransack'

In your ApplicationHlper:

module ApplicationHelper
  include SimpleFormRansackHelper

In "application.rb" (for translations):

class Application
  # It's important that available locales is defined before calling SimpleFormRansack.locale_files
  config.i18n.available_locales = [:da, :en]

  config.i18n.load_path += SimpleFormRansack.locale_files


In your controller:

class CustomerController < ApplicationController
  def index
    @ransack_params = params[:q] || {}
    @ransack = Customer.ransack(@ransack_params)
    @customers = @ransack.result

In your views:

<%= simple_search_form_for @ransack do |f| %>
  <%= f.input :name_cont %>
  <%= f.submit %>
<% end %>

Instead of something like this with a combination of Ransack and SimpleForm:

<%= search_form_for @ransack, builder: SimpleForm::FormBuilder do |f| %>
  <%= f.input :name_cont, label: t(".attribute_contains", attribute: User.human_attribute_name(:name)), input_html: {value: @ransack_params[:name_cont]} %>
  <%= f.submit %>
<% end %>

The name_cont input field now gets it label, hint and more translated as normally for a input with name but functions with the input-name q[name_cont], making it much easier to use Ransack and SimpleForm, because you don't have to give label, hint or anything else manually.

Cookies help us deliver our services. By using our services, you agree to our use of cookies Learn more