未定义的方法`过滤器&#39;对于#<class:0x007fc0600be140>

时间:2016-09-28 12:56:07

标签: ruby-on-rails ruby ruby-on-rails-4

我已收到另一位question的帮助,但现在我有这个错误

undefined method `filter' for #<Class:0x007fc0600be140>`

我试图按类别过滤(类别是在控制台中创建的,比如Category.create(名称:&#34; Ruby&#34;)等等......)

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">]>

我的模型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

我的tutos控制器

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

视图索引

.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"

编辑和新麻烦......

我已将self_filter方法移动到我的类别模型中,即使我仍然无法按类别进行过滤,它看起来更好......当我选择例如&#34; heroku&#时34;一切都还在这里......可能出现什么问题?

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

1 个答案:

答案 0 :(得分:0)

这一行

Category.filter(params[:filter])

失败,因为您根本没有为filter类定义方法Category

修改

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