NameError - 未初始化的常量

时间:2015-08-06 14:29:52

标签: ruby-on-rails ruby-on-rails-4

我使用Ruby on Rails 4,我的项目是关于HR应用程序。员工可以通过他的个人资料页面创建请假申请但是我在他的个人资料页面中创建了表单,我只想创建一个link_to请求控制器但是我有这个错误:

NameError in Employees#show Showing /home/user/workspace/hrapp/app/views/employees/show.html.erb where line #50 raised:

uninitialized constant Requests’

Extracted source (around line #50):

48 </p>
49 <%= link_to 'Edit', edit_employee_path(@employee) %> 
50 <%= link_to ‘My Annual Leave Requests’, controller:'requests' %>
51 <%= link_to 'Back', employees_path %>  

我试过这个解决方案:https://hackhands.com/rails-nameerror-uninitialized-constant-class-solution/

但它没有用,我没有找到这个错误的通用答案。

以下是佣金路线:

user@snf-34155:~/workspace/hrapp$ rake routes
           Prefix Verb   URI Pattern                                         Controller#Action
employee_requests GET    /employees/:employee_id/requests(.:format)          requests#index
                  POST   /employees/:employee_id/requests(.:format)          requests#create
                  PUT    /employees/:employee_id/requests/:id(.:format)      requests#update
                  DELETE /employees/:employee_id/requests/:id(.:format)      requests#destroy
        employees GET    /employees(.:format)                                employees#index
                  POST   /employees(.:format)                                employees#create
     new_employee GET    /employees/new(.:format)                            employees#new
    edit_employee GET    /employees/:id/edit(.:format)                       employees#edit
         employee GET    /employees/:id(.:format)                            employees#show
                  PATCH  /employees/:id(.:format)                            employees#update
                  PUT    /employees/:id(.:format)                            employees#update
                  DELETE /employees/:id(.:format)                            employees#destroy
             root GET    /                                                   welcome#index

我认为我只需要写出正确的路线,但我尝试了很少的路线,但无论如何它都没有用。

拜托,有人可以帮忙吗?

提前致谢:)

应用/控制器/ requests_controller.rb

class RequestsController < ApplicationController
def index
  @employee = Employee.find(params[:employee_id])
  @request = @employee.requests.all
end

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

def new
  @employee = Employee.find(params[:employee_id])
  @request = @employee.requests.new
end

 def create
    @employee = Employee.find(params[:employee_id])
    @request = @employee.requests.create(request_params)
    redirect_to show_employee_path(@employee)
  end

  def destroy
    @employee = Employee.find(params[:employee_id])
    @request = @employee.requests.find(params[:id])
    @request.destroy
    redirect_to show_employee_path(@employee)
  end

  private
    def request_params
      params.require(:request).permit(:start_date, :end_date, :considerations)
    end
end

1 个答案:

答案 0 :(得分:1)

从您发布的代码

</p>
<%= link_to 'Edit', edit_employee_path(@employee) %> 
<%= link_to ‘My Annual Leave Requests’, controller:'requests' %>
<%= link_to 'Back', employees_path %>  

似乎‘My Annual Leave Requests’使用特殊引号字符。你有没有机会从其他来源复制粘贴?

我可以看到的修正是手动将引号更改为使用双引号"

我注意到你的错误未初始化的常量请求'最后还包含一个引号。您是否尝试修复控制器:“请求”?