Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/58.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 On Rails_Routing_Routes_Ruby On Rails 4 - Fatal编程技术网

Ruby on rails 如何向路由添加额外的资源操作?

Ruby on rails 如何向路由添加额外的资源操作?,ruby-on-rails,routing,routes,ruby-on-rails-4,Ruby On Rails,Routing,Routes,Ruby On Rails 4,我有一个名为志愿者的资源: class VolunteersController < ApplicationController before_filter :authenticate_user! def index #@volunteers = Volunteer.all() @volunteers = Volunteer.paginate(:page => params[:page]) end def show @volunteer

我有一个名为志愿者的资源:

class VolunteersController < ApplicationController

  before_filter :authenticate_user!

  def index
    #@volunteers = Volunteer.all()
    @volunteers = Volunteer.paginate(:page => params[:page])
  end

  def show
    @volunteer = Volunteer.find(params[:id])
  end

  def new
    @volunteer = Volunteer.new
  end

  def create
    @post = Volunteer.new(volunteer_params)

    @post.save
    redirect_to @post
  end

  def destroy
    @volunteer = Volunteer.find(params[:id])
    @volunteer.destroy
    redirect_to :action => 'index'
  end

  def search
    @volunteers = Volunteer.search params[:search]
  end

private

  def volunteer_params
    params.require(:volunteer).permit(:forename, :surname)
  end

end

如何映射到搜索操作(包括参数)?

路由文件本身有一个示例:

  # Example resource route with options:
  #   resources :products do
  #     member do
  #       get 'short'
  #       post 'toggle'
  #     end
  #
  #     collection do
  #       get 'sold'
  #     end
  #   end
您需要添加
search
方法作为资源的post(或任何适当的)选项,您可以将其添加到集合或成员-集合方法不会在url中查找特定的项目id,这适用于搜索方法

resources :volunteers do 
  collection do
    post :search
  end
end

有没有办法提到我希望
志愿者/1/搜索
映射到
搜索
操作?@lokesh将
获取“搜索”
添加到志愿者下的
成员do
块。
resources :volunteers do 
  collection do
    post :search
  end
end