Skip to content

Instantly share code, notes, and snippets.

@Krishna
Last active February 5, 2016 20:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Krishna/7fd4007d5a5a2ecf2614 to your computer and use it in GitHub Desktop.
Save Krishna/7fd4007d5a5a2ecf2614 to your computer and use it in GitHub Desktop.
Problems with routing based on roles of a User model
<p id="notice"><%= notice %></p>
<h1>Listing Employees</h1>
<table>
<thead>
<tr>
<th colspan="5"></th>
</tr>
</thead>
<tbody>
<% @employees.each do |employee| %>
<tr>
<td><%= employee.name %></td>
<td><%= employee.email %></td>
<td><%= link_to 'Show', employee %></td>
<td><%= link_to 'Edit', edit_employee_path(employee) %></td>
<td><%= link_to 'Destroy', employee, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<%= link_to 'New Employee', new_employee_path %>
class EmployeesController < ApplicationController
before_action :authenticate_user!
before_action :set_employee, only: [:show, :edit, :update, :destroy]
def index
@employees = current_user.employees.all # I guess the problem is that this returns a collection
# of User objects. But the url_helper will expect objects
# of class Employee (which does not exist).
end
undefined method `user_path' for #<#<Class:0x007f80193c8778>:0x007f8011fd7f30>
Extracted source(around line #17):
<td><%= employee.name %></td>
<td><%= employee.email %></td>
<td><%= link_to 'Show', employee %></td>
<td><%= link_to 'Edit', edit_employee_path(employee) %></td>
<td><%= link_to 'Destroy', employee, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
Prefix Verb URI Pattern Controller#Action
user_session POST /users/sign_in(.:format) devise/sessions#create
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
user_password POST /users/password(.:format) devise/passwords#create
new_user_password GET /users/password/new(.:format) devise/passwords#new
edit_user_password GET /users/password/edit(.:format) devise/passwords#edit
PATCH /users/password(.:format) devise/passwords#update
PUT /users/password(.:format) devise/passwords#update
cancel_user_registration GET /users/cancel(.:format) registrations#cancel
user_registration POST /users(.:format) registrations#create
new_user_registration GET /users/sign_up(.:format) registrations#new
edit_user_registration GET /users/edit(.:format) registrations#edit
PATCH /users(.:format) registrations#update
PUT /users(.:format) registrations#update
DELETE /users(.:format) registrations#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
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_many :roles # employee or manager
has_many :employee_engagements_as_manager, class_name: "EmployeeEngagement", foreign_key: :manager_id
has_many :employees, through: :employee_engagements_as_manager
def self.all_employees
User.joins(:roles).where(roles: {role:'employee'}).uniq
end
# ....snip....
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment