我有一个叫做员工的控制器。
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
这意味着它对节目动作的影响。我不知道为什么。有什么解决方案。谢谢
答案 0 :(得分:1)
您的route.rb文件中的路由有点奇怪。只需使用指定自定义操作的通用方法,方法是将其添加到资源块:
resources :employees do
collection do
get :admission
end
end
答案 1 :(得分: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
,并且您未在:id
中传递link_to
。错误也是如此。
答案 2 :(得分: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
路线(其中单< / em>处理数据记录):
#config/routes.rb
resources :employees do
get :admission, as: :collection
end
这与您对Rails path helpers的禁欲相结合,这将有助于您相应地路由您的请求:
<%= link_to 'admission', employees_admission_path %>