-1

How do I display my employee nonavailability table in my schedule view?

As I am creating a schedule for my employees I would like to see my employee nonavailability table in the view of my schedule model.

<div class="col-lg-6 col-sm-12 right">
      <% @non_availabilities = NonAvailability.all %>
      <table class="table table-striped table-bordered table-hover">
        <thead>
        <tr>
          <th class="col-md-2">Employee</th>
          <th class="col-md-2">Date</th>
          <th class="col-md-2">Time</th>
          <th class="col-md-2">Reason</th>
        </tr>
        </thead>
      <tbody>
      <% @non_availabilities.each do |non_availability| %>
        <tr>
        <td class="col-md-2"><%= non_availability.employee.full_name %></td>
        <td class="col-md-2"><%= non_availability.date %></td>
        <td class="col-md-2"><%= non_availability.time %></td>
        <td class="col-md-2"><%= non_availability.reason %></td></tr>
      <% end %>
        </tbody>
        </table>
    </div>
Jovany
  • 35
  • 8

3 Answers3

1

Let's say your employee table is a model called Employee and your schedule form is a model called Schedule.

If you wanted to list all of your employees in your schedules show view (or any other view such as index), you can use something like this:

class SchedulesController < ApplicationController

  def show
    @employees = Employee.all
  end

end

In "Employee.rb" (model): belongs_to :schedule

In "Schedule.rb" (model): has_many :employees

And in your shedules show.html.erb:

<% @employees.each do |employee| %>
  <p><%= employee.name %></p>
  <p><%= employee.position %></p>
<% end %>
Jake
  • 1,086
  • 12
  • 38
  • I attempted to make it work with this code but I get the following error "undefined method 'each' for nil:NilClass". It highlights the <%@employee_table.each do |employee| %> part – Jovany Oct 06 '18 at 17:51
  • @Jovany What is the name of the two models you are trying to link together? I've updated my answer, I forgot to include the relationships between each table. – Jake Oct 06 '18 at 19:52
  • Schedule.rb and employee.rb are the model names – Jovany Oct 07 '18 at 16:12
  • @Jovany Cool, this solution should work. If it does don't forget to accept it as the answer, thanks! – Jake Oct 08 '18 at 12:26
  • well I attempted and it doesn't work for me. Maybe I should reclarify. I want the table of employees to show up on the form view of schedule. As I create a schedule I want to be able to see the list of employees. – Jovany Oct 08 '18 at 20:50
  • @Jovany I still am not quite sure what you're going for. Please update your question with what code you have right now and a further explanation of what exactly you want where. – Jake Oct 09 '18 at 00:54
  • I honestly don't know how I can explain it any better. I assume you know what the form view is. I want to split the view in half. Half the page will be the form to create a schedule and the other half will be displaying the table of employees. the code I have is what you give me, but I want the table to display in the Shedules _form.html.erb – Jovany Oct 09 '18 at 04:59
1

It was a very simple answer to my issue. To display the table of a model on a different model view all I needed was the following.

<% @employees.each do |employee| %>
  <%= employee.name %>
  <%= employee.position %>
<% end %>
Jovany
  • 35
  • 8
0

The answer by @Jake is correct, just one caveat.

Let's assume you define a schedule as two-week roster, therefore relationship would become a many to many (not belongs_to) and you will need a join table.

In "Schedule.rb" (model): has_and_belongs_to_many :employes In "Employee.rb" (model): has_and_belongs_to_many :schedules

I had a similar problem last week, this answer was helpful: Creating a many to many relationship in Rails