Hello, I have some issue with Ransack I'm making ...
# support
s
Hello, I have some issue with Ransack I'm making a new admin panel with search bar using ransack like in
admin/products/index.html.erb
but I get the error
undefined method 'name_cont' for Ransack::Search<class: Spree::Kit, base: Grouping <combinator: and>>:Ransack::Search
Copy code
# frozen_string_literal: true

module Spree
  module Admin
    class NewPanelController < ResourceController
      update.before :update_before
      helper_method :clone_object_url
      before_action :split_params, only: [:create, :update]
      
      def show
        redirect_to action: :edit
      end

      def index
        session[:return_to] = request.url
        respond_with(@collection)
      end

      def destroy
        ...
      end

      def clone
        ...
      end
      
      private
      
      def collection
        return @collection if @collection
        params[:q] ||= {}
        params[:q][:s] ||= "name asc"
        # @search needs to be defined as this is passed to search_form_for
        @search = super.ransack(params[:q])
        @collection = @search.result.
              order(id: :asc).
              includes(:images).
              page(params[:page]).
              per(Spree::Config[:admin_kits_per_page])
      end
      
      def split_params
      end
      
      def update_before
         ...
      end
      
      def clone_object_url(resource)
        clone_admin_kit_url resource
      end
    end
  end
end
index.html.erb
Copy code
<%= search_form_for [:admin, @search] do |f| %>
        <%- locals = {f: f} %>
        <div class="row" data-hook="admin_kits_index_search" >
          <div class="col-10">
            <div class="field">
              <%= f.label :name_cont, Spree::Kit.human_attribute_name(:name) %>
              <%= f.text_field :name_cont, size: 15 %>
            </div>
          </div>

          <div class="col-2">
            <div class="field checkbox">
              <label>
                <%= f.check_box :with_discarded, { checked: params[:q][:with_discarded] == 'true', class: 'js-with-discarded-input' }, 'true', 'false' %>
                <%= t('spree.show_deleted') %>
              </label>
            </div>
          </div>
        </div>

        <div class="actions filter-actions" data-hook="admin_kits_index_search_buttons">
          <%= button_tag t('spree.search'), class: 'btn btn-primary' %>
        </div>
    <% end %>
k
Hello. Did you already allowed the
name
field to be “ransackable” as explained here: https://activerecord-hackery.github.io/ransack/going-further/other-notes/#authorization-allowlistingdenylisting?
👍 1
r
If you want to allow attributes for a Solidus model(or one that just inherits Spree::Base), you can also use
allowed_ransackable_attributes
like Spree::Product does: https://github.com/solidusio/solidus/blob/495772b91f13d8c928041fbcc4fa3e6a54408caa/core/app/models/spree/product.rb#L135 Note, these attributes changed a little bit in the last few versions, so you’ll need to check how these solidus methods are created in the Solidus version you are using: https://github.com/solidusio/solidus/blob/master/core/app/models/concerns/spree/ransackable_attributes.rb#L6-L8
👍 1
s
Indeed it was a permission error 😉 Thank you for your quick replies @kennyadsl @Ryan Woods 🙂
k
You are welcome!