Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I'm having issues printing the list of items when using filterrific. #200

Open
rmezapi opened this issue May 25, 2021 · 0 comments
Open

Comments

@rmezapi
Copy link

rmezapi commented May 25, 2021

When I go to print the table, I get the following errors. One of them is an undefined method for each and the other is a difficulty rendering in thebreweries/index.html.erb file. I think that I am getting an entry list of breweries. I tried a fix you had by changing the rails_admin per model but was unable to do it, as I am currently not using the 'rails_admin' gem. I would really appreciate some tips and fixes for this issue as it it my first time using rails and filterrific. Thanks so much!
Here is my code for the breweries controller and the breweries model, as well as the _list and index HTML file.

`class BreweriesController < ApplicationController

def index
    # @brewery = Brewery.all
    (@filterrific = initialize_filterrific(
        Brewery,
        params[:filterrific],
        select_options: {
            sorted_by: Brewery.options_for_sorted_by,
            status_show: Brewery.options_for_status_show,
        },
    )) || return 
    @breweries = @filterrific

    respond_to do |format|
        format.html
        format.js
    end
end

def new
    @brewery = Brewery.new
end

def create
    @brewery = Brewery.new(brewery_params)
    # Check is user is saved
    if @brewery.save
        redirect_to breweries_path
    else
        render 'new'
    end
end

def update
    @brewery = Brewery.find(params[:id])
    @brewery.update(brewery_params)
    redirect_to breweries_path
end

def edit
    @brewery = Brewery.find(params[:id])
end

def destroy
    @brewery = Brewery.find(params[:id])
    @brewery.destroy
    redirect_to breweries_path
end

def show
    @brewery = Brewery.find(params[:id])
    @beers = Beer.where(brewery_id:@brewery.id)
end

private def brewery_params
    params.require(:brewery).permit(:brewery_name, :year_founded, 
                                    :city, :zip_code, :logo, :county, :state, :control_group_production, :beer_num,
                                    :beer_styles, :beers_per_style, :prod_per_num_beers, :median_income, :poverty_rate,
                                    :status, :population, :urban_or_rural)
end

def search
    if params[:search].blank?
        redirect_to(root_path, alert: "No results") and return
    else
        @parameter = params[:search].downcase
        @results = Brewery.all.where("lower(brewery_name) LIKE :search", search: "%#{@parameter}%")
    end
end

end`

`class Brewery < ActiveRecord::Base

filterrific :filter_params =>  {:sorted_by => 'founded_in_asc' },
            :available_filters => %w[
                sorted_by
                status_show
                by_zip
                in_county
                in_city
            ]

# # pagination
# self.per_page = 25

has_many :beers

scope :sorted_by, ->(sort_option){
    direction = (sort_option =~ /asc$/) ? 'asc' : 'desc'
    breweries = Brewery.arel_table
    case sort_option.to_s 
    when /^founded_in_/
        # order the breweries in direction
        order(breweries[:founded_in].send(direction))
    when /^brewery_name_/
        #order alphabetically
        order(breweries[:brewery_name].lower.send(direction))
    else
        raise(ArgumentError, "Invalid sort option: #{sort_option.inspect}")
    end
}

scope :status_show, ->(statuses){
    where(breweries[:status] => [*statuses])
}

scope :by_zip, -> (zip){
    return nil if zip.blank?
    terms = zip.downcase.split(/\s+/)
    # replace "*" with "%" for wildcard searches,
    # append '%', remove duplicate '%'s
    terms = terms.map { |e|
    (e.gsub('*', '%') + '%').gsub(/%+/, '%')
    }
    # configure number of OR conditions for provision
    # of interpolation arguments. Adjust this if you
    # change the number of OR conditions.
    num_or_conditions = 3
    where(
    terms.map {
        or_clauses = [
        "LOWER(breweries.zip_code) LIKE ?"
        ].join(' OR ')
        "(#{ or_clauses })"
    }.join(' AND '),
    *terms.map { |e| [e] * num_or_conditions }.flatten
    )
}

scope :in_county, ->(counties){
    where(breweries[:county] => [*counties])
}

scope :in_city, ->(cities){
    where(breweries[:city] => [*cities])
}

def self.options_for_sorted_by
    [
        ['Alphabetical by Name', 'brewery_name_asc'],
        ['Year Founded (Oldest First)', 'founded_in_asc'],
        ['Year Founded (Newest First)', 'founded_in_desc'],
    ]

end

def self.options_for_status_show
    [
        ['O', 'founded_in_asc'],
        ['C', 'founded_in_asc'],
    ]
end

end`

_list.html.erb
`

Breweries

<% @breweries.each do |brewery|%>

<% end %>
Logo Brewery Status Zip Code Year Founded Edit
<%= brewery.logo %> <%= link_to brewery.brewery_name, brewery_path(brewery) %> <%= brewery.status %> <%= brewery.zip_code %> <%= brewery.year_founded %> <%= link_to "Edit", edit_brewery_path(brewery) %>
`

breweries/index.html.erb
`
<%= form_for_filterrific @filterrific do |f| %>

Sort by <%= f.select( :sorted_by, @filterrific.select_options[:sorted_by], { include_blank: '- Any -' } ) %>
By Zip Code <%= f.text_field(:by_zip, class: 'filterrific-periodicall-observed') %>
Show Only <%= f.select(:status_show, @filterrific.select_options[:status_show]) %>
By County <%= f.text_field(:in_county, class: 'filterrific-periodicall-observed') %>
By City <%= f.text_field(:in_city, class: 'filterrific-periodicall-observed') %>
<%= link_to( 'Reset filters', reset_filterrific_url, ) %>
<%# add an automated spinner to your form when the list is refreshed %> <% end %>

<%= render(
partial: 'breweries/list',
locals: { breweries: @breweries }
) %>`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant