Form helper for custom ransacker args - Rails
Asked Answered
D

0

7

According to Ransack documentation, there is a possibility to pass custom arguments to a ransacker method:

class Person < ApplicationRecord
  ransacker :author_max_title_of_article_where_body_length_between, args: [:parent, :ransacker_args] do |parent, args|
    min, max = args
    query = <<-SQL
      (SELECT MAX(articles.title)
       FROM articles
       WHERE articles.person_id = people.id
         AND CHAR_LENGTH(articles.body) BETWEEN #{min.to_i} AND #{max.to_i}
       GROUP BY articles.person_id
      )
    SQL
    Arel.sql(query)
  end
end

In the same documentation there is even an example of passing custom arguments:

Person.ransack(
  conditions: [{
    attributes: {
      '0' => {
        name: 'author_max_title_of_article_where_body_length_between',
        ransacker_args: [10, 100]
      }
    },
    predicate_name: 'cont',
    values: ['Ransackers can take arguments']
  }]
)

However, there is no documentation on how can I pass that custom arguments from a view, as there appears to be no search_form_for helpers to do that.

Is there a way of passing these arguments through the form, without needing to parse the parameters in the controller to perform the search as pointed in the example?

Dessiatine answered 19/7, 2019 at 21:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.