Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/52.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Ruby Rails:是否可以在ActionController中重载索引方法?_Ruby On Rails_Ruby_Controller_Indexing_Overloading - Fatal编程技术网

Ruby on rails Ruby Rails:是否可以在ActionController中重载索引方法?

Ruby on rails Ruby Rails:是否可以在ActionController中重载索引方法?,ruby-on-rails,ruby,controller,indexing,overloading,Ruby On Rails,Ruby,Controller,Indexing,Overloading,我想要下面这样的东西 class CompanyController < ApplicationController def index #return all of companies end def index #return companies based filter on company :name, :location, :type (any combination of these

我想要下面这样的东西

    class CompanyController < ApplicationController
       def index
           #return all of companies 
       end
       def index
           #return companies based filter on company :name, :location, :type (any combination of these)
       end
    end

你不能这样做,但你可以这样做:

class CompanyController < ApplicationController
   def index
      if params[:name] # add ifs etc
        @companies =  Company.where(:name => params[:name])
      else
        @companies = Company.all
      end
   end
end

我想这就是你的意思,如果我错了,告诉我

你不能这样做,但你可以这样做:

class CompanyController < ApplicationController
   def index
      if params[:name] # add ifs etc
        @companies =  Company.where(:name => params[:name])
      else
        @companies = Company.all
      end
   end
end

我想这就是你的意思,如果我错了,告诉我

在Ruby中不能有两个同名的方法。如果您有多个同名的方法,那么最后定义的方法将是Ruby使用的方法。

在Ruby中不能有两个同名的方法。如果您有多个同名的方法,那么最后定义的方法将是Ruby使用的方法。

我已经按照您的方式实现了这一点。您还可以告诉我或参考链接,了解如何根据输入参数重载index方法,无论它是:id还是:companyy。如果您不能,请使用if语句。我已经按照您的方式实现了这一点。您还可以告诉我或参考链接,了解如何根据输入参数重载索引方法,无论它是:id还是:companyy您不能,只需使用if语句即可