0

I'm creating a join table that will show me which employee sold which comic. When I type in my employee name It is giving me this error

NoMethodError in Showemployeesales#employeesaleout
undefined method `name_id' for nil:NilClass

Here is my code for my controller

 def employeesaleout
 @employee_name = params[:employee_name_in]
 r = Employee.find_by_name_id(@employee_name)
 @sale_list = r.sales
end

Here is my code for my input view "employeenamein"

   <h1>Showemployeesales#employeenamein</h1>
   <p>Find me in app/views/showemployeesales/employeenamein.html.erb</p>
    <%= form_tag(showemployeesales_employeesaleout_path, :controller => 
    "showemployeesales", :action => "employeesaleout", :method => "post") do 
      %>

    <div class="field">
      <%= label_tag :Employee_Name %><br />
        <%= text_field_tag :employee_name_in %>
    </div>

    <div class="actions">
          <%= submit_tag "Submit Employee Name" %>
    </div>
    <% end %>

Here is my code for my output view

    <center><h1>These are the Sales for <%= @employee_name %> </h1></center>
    <br /> <br />

     <center><table width = 65% border = 1> 
     <tr> <th> Comic Name </th><th> Comic ID </th></tr>   
     <% @sale_list.each do |m| %>     

     <tr> <td> <%= m.product.name_id %> </td> <td> <%= m.product.id_no %> 
     </td></tr>
     <% end %> </table> </center><br /> <br />

And my products table under my schema

   create_table "products", force: :cascade do |t|
   t.string   "name_id"
   t.integer  "id_no"
   t.datetime "created_at", null: false
   t.datetime "updated_at", null: false
   end

What am I doing wrong?

  • You must have a `product` without a `name_id`. The simplest thing to do, is to view your data and update that field. – Alejandro Montilla May 22 '17 at 22:17
  • I'm sorry but I don't know what you're getting at. What am I supposed to do? I'm fairly new at Ruby on Rails. In my Products table I have 2 entries with a name and an Id and nothing is blank. – Jeremiah Mall May 22 '17 at 22:52
  • @AlejandroMontilla I have been coding RoR for years and have no idea what you are getting at. – max May 22 '17 at 23:06

1 Answers1

0

You are really overcomplicating it.

To view data in REST you use a GET request, not post. And the naming is ... not good.

# routes.rb
resources :employees do
  resouces :sales, only: :index, module: :employees
end

# app/controllers/employees/sales_controller.rb
class Employees::SalesController
  # /employees/:employee_id/sales
  def index
    @employee = Employee.includes(:sales)
                        .find(params[:id])
    @sales = @employee.sales
  end
end

Just create a list of employees and have a link for each:

<% @employees.each do |e| %>
  <%= link to e.name, employee_sales_path(e) %>
<% end %>

Or use a form with autocomplete. But really KISS at your level.

# app/views/employees/sales/index.html.erb
# use CSS instead of <center> and missusing <br> tags - it's not 1998
<h1>These are the Sales for <%= @employee.name %></h1>
<table> 
  <tr>
    <th>Comic Name</th>
    <th> Comic ID</th>
  </tr>   
  <% @sales.each do |s| %>     
  <tr>
    <td><%= s.product.name_id %></td>
    <td><%= s.product.id_no %></td>
  </tr>
  <% end %> 
</table>
max
  • 96,212
  • 14
  • 104
  • 165