bogdan / datagrid

Gem to create tables grids with sortable columns and filters
MIT License
1.02k stars 115 forks source link

Receiving a 302 error when searching #308

Closed mkwoodburn closed 1 year ago

mkwoodburn commented 1 year ago

I have an issue when I'm trying to use search functionality. I am running a Rails 5 project on Ruby 2.7.2. When I inspect the request and response the correct datagrid is returned, but the page does not reload with the new datagrid table. If you would like more detail please let me know.

I have another side project that I am using datagrids and the search works perfectly.

Do you have any ideas why this would happen?

I have really enjoyed the functionality of this library, thank you all for the time and effort you have put in to develop it.

Thanks in advance Matthew Woodburn

bogdan commented 1 year ago

Hey, back in the good old days we were saying that our "telepathy team is on vacation".

I need to see your grid, controller and view source code.

mkwoodburn commented 1 year ago

Ok, sounds good. I can send you all three. - I did rename all the variables.

The error I'm getting in both Safari and Chrome is a 302.


Controller

def search
  # params[:param1].empty? & params[:param2].empty? & !params[:param3].empty?
  if !params[:param1].empty? & params[:param2].empty? & params[:param3].empty?
    @controller_grid =
    ControllersGrid.new(params[:controllers_grid]) do |scope|
      scope.where(id: controller.where(
        controller_type_id: ControllerType.ids[:param4],
        param1_id: Param1.where(name: params[:param1]).ids.first
      ).ids)
    end
end

Datagrid

class ControllersGrid
  include Datagrid

  scope { Controller }

  column(:params1_id) do |model|
    Param1.find(model.param1_id).name
  end
  column(:params5)
  column(:param6)
  column(:param7)
  column(:param8) do
    format('$ %0.2f', param8.to_s)
  end
  column(:param9)
  column(:param10)
  column(:id, header: 'Controller Id')
  column(:param3, header: 'param3 Id')
end

search.html.erb

<%= render "search" %>

<%= datagrid_table(@offer_grid) %>

_search.html.erb

<%= form_with url: 'path', method: :get do |form| %>
  <div class="offer-search-inline">
    <%= form.label :param1, "Filter for param1" %>
    <%= form.text_field :param1 %>
  </div>
  <div class="offer-search-inline">
    <%= form.label :param2, "Filter for param2" %>
    <%= form.text_field :param2 %>
  </div>
  <div class="offer-search-inline">
    <%= form.label :param3, "Filter for param3" %>
    <%= form.text_field :param3 %>
  </div>
  <div class='search-button offer-search-inline'>
    <%= form.submit "Filter" %>
  </div>
<% end %>

route

resources :controller do
  get 'search', on: :collection
end
bogdan commented 1 year ago

You have a lot of typos:

- <%= datagrid_table(@offer_grid) %>
+ <%= datagrid_table(@controller_grid) %>
- if !params[:param1].empty? & params[:param2].empty? & params[:param3].empty?
+ if !params[:param1].empty? || !params[:param2].empty? || !params[:param3].empty?

Not sure if I got your logical condition correctly, but it looks completely wrong....

mkwoodburn commented 1 year ago

I got it figured out thanks.