Module: Ransack::Helpers::FormHelper
- Defined in:
- lib/ransack/helpers/form_helper.rb
Defined Under Namespace
Classes: SortLink
Instance Method Summary collapse
-
#search_form_for(record, options = {}, &proc) ⇒ Object
search_form_for. -
#search_form_with(record_or_options = {}, options = {}, &proc) ⇒ Object
search_form_with. -
#sort_link(search_object, attribute, *args, &block) ⇒ Object
sort_link. -
#sort_url(search_object, attribute, *args) ⇒ Object
sort_url<%= sort_url(@q, :created_at, default_order: :desc) %>. -
#turbo_search_form_for(record, options = {}, &proc) ⇒ Object
turbo_search_form_for.
Instance Method Details
#search_form_for(record, options = {}, &proc) ⇒ Object
search_form_for
<%= search_form_for(@q) do |f| %>
9 10 11 12 13 14 15 |
# File 'lib/ransack/helpers/form_helper.rb', line 9 def search_form_for(record, = {}, &proc) search = extract_search_and_set_url(record, , 'search_form_for') [:html] ||= {} = (search, , :get) (, ) form_for(record, , &proc) end |
#search_form_with(record_or_options = {}, options = {}, &proc) ⇒ Object
search_form_with
<%= search_form_with(model: @q) do |f| %>
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/ransack/helpers/form_helper.rb', line 21 def search_form_with( = {}, = {}, &proc) if .is_a?(Hash) && .key?(:model) # Called with keyword arguments: search_form_with(model: @q) = record = .delete(:model) else # Called with positional arguments: search_form_with(@q) record = end search = extract_search_and_set_url(record, , 'search_form_with') [:html] ||= {} = (search, , :get) (, ) form_with(model: search, **, &proc) end |
#sort_link(search_object, attribute, *args, &block) ⇒ Object
sort_link
<%= sort_link(@q, :name, [:name, 'kind ASC'], 'Player Name') %>
You can also use a block:
<%= sort_link(@q, :name, [:name, 'kind ASC']) do %>
<strong>Player Name</strong>
<% end %>
65 66 67 68 69 70 71 72 73 |
# File 'lib/ransack/helpers/form_helper.rb', line 65 def sort_link(search_object, attribute, *args, &block) search, routing_proxy = extract_search_and_routing_proxy(search_object) unless Search === search raise TypeError, 'First argument must be a Ransack::Search!' end args[args.first.is_a?(Array) ? 1 : 0, 0] = capture(&block) if block_given? s = SortLink.new(search, attribute, args, params, &block) link_to(s.name, url(routing_proxy, s.), s.(args)) end |
#sort_url(search_object, attribute, *args) ⇒ Object
sort_url <%= sort_url(@q, :created_at, default_order: :desc) %>
78 79 80 81 82 83 84 85 |
# File 'lib/ransack/helpers/form_helper.rb', line 78 def sort_url(search_object, attribute, *args) search, routing_proxy = extract_search_and_routing_proxy(search_object) unless Search === search raise TypeError, 'First argument must be a Ransack::Search!' end s = SortLink.new(search, attribute, args, params) url(routing_proxy, s.) end |
#turbo_search_form_for(record, options = {}, &proc) ⇒ Object
turbo_search_form_for
<%= turbo_search_form_for(@q) do |f| %>
This is a turbo-enabled version of search_form_for that submits via turbo streams instead of traditional HTML GET requests. Useful for seamless integration with paginated results and other turbo-enabled components.
45 46 47 48 49 50 51 52 53 |
# File 'lib/ransack/helpers/form_helper.rb', line 45 def turbo_search_form_for(record, = {}, &proc) search = extract_search_and_set_url(record, , 'turbo_search_form_for') [:html] ||= {} = () method = .delete(:method) || :post = (search, , method).merge() (, ) form_for(record, , &proc) end |