Taly
Taly

Reputation: 35

NameError - uninitialized constant

I'm using Ruby on Rails 4 and my project is about an HR App. An employee can create a leave request through his profile page BUT instead I create the form in his profile page, I only wanted to create a link_to the requests controller BUT I've got this error:

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 %>  

AND I tried this solution: https://hackhands.com/rails-nameerror-uninitialized-constant-class-solution/

BUT it didn't work and I didn't find a generic answer for this error.

Here is the rake routes:

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

I think I just need to write the right route but I tried few of them and it didn't work anyway.

Please, can anybody help?

Thanks in advance :)

app/controllers/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

Upvotes: 2

Views: 4086

Answers (1)

Leo Correa
Leo Correa

Reputation: 19789

From the code you posted

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

It seems that the ‘My Annual Leave Requests’ is using special quote characters. Did you by any chance copy and paste that from another source?

A fix I can see is manually changing the quotes to using double quotes "

I noticed your error uninitialized constant Requests’ also contains a quote at the end. Have you try fixing the controller:"requests" as well?

Upvotes: 1

Related Questions