2013-10-17 123 views
1

我有一個名爲資源志願者:如何向我的路線添加其他資源操作?

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 

我增加了一個額外的動作稱爲搜索,但我有問題,它在我的路線文件映射:

root 'home#index' 
resources :volunteers 

如何映射到搜索行動(包括參數)?

回答

4

沒有在路由文件本身就是一個例子:

# Example resource route with options: 
    # resources :products do 
    #  member do 
    #  get 'short' 
    #  post 'toggle' 
    #  end 
    # 
    #  collection do 
    #  get 'sold' 
    #  end 
    # end 

您需要添加search方法作爲後(或任何爲宜)的資源選項,你可以將它添加到收藏或成員 - 收集方法不會在url中查找特定的項目標識,這適用於搜索方法。

resources :volunteers do 
    collection do 
    post :search 
    end 
end 
+0

有沒有辦法提及我希望'志願者/ 1/search'映射到'search'動作? – Lokesh

+1

@lokesh將'get'search''添加到志願者的'member do'塊中。 – Matt

+0

謝謝!有效。 – Lokesh