Last active
February 5, 2016 20:17
-
-
Save Krishna/7fd4007d5a5a2ecf2614 to your computer and use it in GitHub Desktop.
Problems with routing based on roles of a User model
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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 %> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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