Nelly-Johan Nelly-Johan - 2 months ago 6
Ruby Question

undefined method `filter' for #<Class:0x007fc0600be140> edited, need more help please

Please see edit below



I've received precious help from another question but now I have this error
undefined method
filter' for #`

I am trying to filter by categories (Category were created in the console, like Category.create(name: "Ruby") and so on...)

Looking forward for your help, thanks in advance :)

2.3.1 :009 > Category.all
Category Load (0.2ms) SELECT "categories".* FROM "categories"
=> #<ActiveRecord::Relation [#<Category id: 1, name: "Ruby", created_at: "2016-09-26 09:03:17", updated_at: "2016-09-26 09:03:17">, #<Category id: 2, name: "Rails4", created_at: "2016-09-26 09:03:25", updated_at: "2016-09-27 14:32:39">, #<Category id: 3, name: "Rails5", created_at: "2016-09-26 09:03:30", updated_at: "2016-09-27 14:35:25">, #<Category id: 4, name: "Heroku", created_at: "2016-09-26 09:03:35", updated_at: "2016-09-27 14:35:47">, #<Category id: 5, name: "AWS-Amazon", created_at: "2016-09-26 09:03:43", updated_at: "2016-09-26 09:03:43">]>


my model tuto

class Tuto < ActiveRecord::Base
acts_as_votable
belongs_to :user
belongs_to :category
validates :category_id, presence: true


def self.search(search)
if search
where(["title LIKE ?","%#{search}%"]).order('created_at DESC')
else
all
end
end
#moved this method in category.rb
#def self.filter(filter)
#if filter
#where(["name LIKE ?","%#{filter}%"]).order('created_at DESC')
#else
#all
#end
#end
end


my tutos controller

class TutosController < ApplicationController
before_action :authenticate_user!, only: [:new, :create]
before_action :set_tuto, only: [:show, :edit, :update, :destroy, :upvote]


def index
#binding.pry
if params[:search].present?
@tutos = Tuto.search(params[:search]).includes(:user, :category)
else
@tutos = Tuto.all.includes(:user, :category)
end

if params[:filter].present?
@categories = Category.filter(params[:filter])
else
@categories = Category.all
end
end


def show
@tuto = Tuto.find(params[:id])
@user = User.all
end

def new
@tuto = Tuto.new
end

def edit
end

def create

@tuto = Tuto.new(tuto_params)
@tuto.user_id = current_user.id

respond_to do |format|
if @tuto.save
flash[:success] = "Test"
format.html { redirect_to @tuto, notice: 'Tuto was successfully created.' }
format.json { render :show, status: :created, location: @tuto }
else
format.html { render :new }
format.json { render json: @tuto.errors, status: :unprocessable_entity }
end
end
end

def update
respond_to do |format|
if @tuto.update(tuto_params)
format.html { redirect_to @tuto, notice: 'Tuto was successfully updated.' }
format.json { render :show, status: :ok, location: @tuto }
else
format.html { render :edit }
format.json { render json: @tuto.errors, status: :unprocessable_entity }
end
end
end


def destroy
@tuto.destroy
respond_to do |format|
format.html { redirect_to tutos_url, notice: 'Tuto was successfully destroyed.' }
format.json { head :no_content }
end
end


def upvote
@tuto.upvote_by current_user
redirect_to :back
end


private


def set_tuto
@tuto = Tuto.find(params[:id])
end

def tuto_params
params.require(:tuto).permit(:title, :content, :id, :user_id, :category_id)
end
end


the view index

.container
.row
h1.text-gray Tutorials
.row.search_banner
.col-xs-3
=form_tag tutos_path, :method => 'get' do
=text_field_tag :search, params[:search], placeholder:"Search by keywords"
=submit_tag "Search", class:'btn btn-xs btn-default btn-search'

.col-xs-3
=form_tag tutos_path, :method => 'get' do
=select_tag :filter, options_for_select(Category.all.map{|c| c.name}, params[:filter])
=submit_tag "Search", class:"btn btn-xs btn-default btn-search"


EDIT and new trouble....



I've moved the
self_filter
method into my category model and it looks better even though I still can't filter by categories... When I select for exemple "heroku" everything is still here... What could be wrong?

class Category < ActiveRecord::Base
has_many :tutos

def self.filter(filter)
if filter
where(["name LIKE ?","%#{filter}%"]).order('created_at DESC')
else
all
end
end
end

Answer

This line

Category.filter(params[:filter])

fails because you simply do not have method filter defined for Category class.

EDIT

where("name LIKE '%#{filter}%'").order(created_at: :desc)