2014-03-28 47 views
0

我有一個名爲員工的控制器。導軌路由和活動記錄錯誤

class EmployeesController < ApplicationController 

    def admission  
    @bank_fields = BankField.all.where(:status => true) 
    if @bank_fields.empty? 
     redirect_to :action => "show", :id => @bank_fields.first.id 
    end  
    end 

    def show 
    @employee = Employee.find(params[:id]) 
    end 

    # The RESTful actions are as usual; I didn't give those here. 
end 
在路線

文件

match ':controller/:action/:id', :via => [:get, :post,:put] 

match ':controller/:action', :via => [:post, :get] 

resources :employees 

在我所謂的入院行動鑑於

<%= link_to 'admission', :controller => :employees, :action => :admission %> 

但是當我按下鏈接我得到了下面的錯誤。

ActiveRecord::RecordNotFound at /employees/admisson 
Couldn't find Employee with id=admisson 

這意味着其擊球的演出action.Which我不知道why.Is有任何solution.Thanks

+0

你缺少的:ID在你的link_to.You應傳遞一個:ID。 – Pavan

+0

感謝您的答覆。我實際上不需要id。我想重定向到 匹配':controller /:action',:via => [:post,:get] 這條路徑。指向 匹配':controller /:action /:id',:via => [:post,:get] 我不想這樣做。 – user3316484

+0

目前你有bankfields的記錄status => true? – Pavan

回答

0

按照意見,如果你想重定向到match ':controller/:action', :via => [:post, :get],只需編輯您的路線如下。

match ':controller/:action', :via => [:post, :get] #primary 

match ':controller/:action/:id', :via => [:get, :post,:put] #secondary 

之前,你有

match ':controller/:action/:id', :via => [:get, :post,:put] #primary 

match ':controller/:action', :via => [:post, :get] #secondary 

所以,主路由需要一個:id,你是不是在你的link_to傳遞一個:id。錯誤也是如此。

+0

謝謝你,那就是我錯過的把戲。謝謝。 – user3316484

+0

@ user3316484樂意幫忙! – Pavan

+0

@ user3316484如果您滿意,請接受答案。 – Pavan

1

在route.rb文件的路徑是有點奇怪。只需將它添加到資源塊使用指定自定義動作的一般方法:

resources :employees do 
    collection do 
    get :admission 
    end 
end 
0

你的問題是你不使用Rails的resourceful routing structure(這是怎麼了Rails的基礎上計算路線):

#config/routes.rb 
resources :employees 
    #-> get /employees, to: "employees#index" 
    #-> get /employees/:id, to: "employees#show", id: :id 
    #-> get /employees/new, to: "employees#new" 
    #-> post /employees/new, to: "employees#create" 
    #-> get /employees/:id/edit, to: "employees#edit" 
    #-> patch /employees/:id, to: "employees#update" 
    #-> delete /employees/:id, to: "employees#destroy" 

可以使用collection方法給你‘集體’航線(其中集合數據的被存儲),或member路由(其中數據記錄處理):

#config/routes.rb 
resources :employees do 
    get :admission, as: :collection 
end 

這是混有你的禁慾從Rails path helpers,這將幫助您將自己的請求相應:

<%= link_to 'admission', employees_admission_path %>