diff --git a/lib/tabulatr/rails/action_controller.rb b/lib/tabulatr/rails/action_controller.rb index 10c3d89..fd1888d 100644 --- a/lib/tabulatr/rails/action_controller.rb +++ b/lib/tabulatr/rails/action_controller.rb @@ -27,13 +27,22 @@ class ActionController::Base end def tabulatr_for(relation, tabulatr_data_class: nil, serializer: nil, render_action: nil, default_order: nil, locals: {}, &block) - table_id = params[:table_id] - if table_id and relation.is_a?(Array) - controller_path = self.class.name.underscore.gsub('/', '-') - idx = relation.index do |r| - table_id.start_with? "#{Tabulatr::Utility.formatted_name(r.name)}_table_#{controller_path}_#{self.action_name}_" + if relation.is_a?(Array) + table_id = params[:table_id] + idx = nil + if table_id + controller_path = self.class.name.underscore.gsub('/', '-') + idx = relation.index do |r| + n = "#{Tabulatr::Utility.formatted_name(r.name)}_table_#{controller_path}_#{self.action_name}_" + table_id.start_with? n + end + end + if idx + relation = relation[idx] + else + render action: render_action || action_name + return end - relation = relation[idx] end klass = relation.respond_to?(:klass) ? relation.klass : relation locals[:current_user] ||= current_user if respond_to?(:current_user)