2

I defined the next things:

task.rb:

class Task < ActiveRecord::Base
   belongs_to :worker
   attr_accessible :done, :name
end

worker.rb:

class Worker < ActiveRecord::Base
  has_many :tasks
  attr_accessible :name
end

I wrote the next code in "views/workers/index.html.erb":

<h1>Listing workers</h1>

<table>
  <tr>
    <th>Name</th>
    <th>Task</th>
    <th>Done</th>
    <th></th>
    <th></th>
    <th></th>
  </tr>

<% @workers.group_by(&:name).each do |name, tasks| %>
  <tr>
    <td><%= name %></td>
    <td><%= tasks.size %></td>
    <td><%= tasks.select{ |task| task.done != 'yes' }.size %></td>
    <td><%= link_to 'new Task', new_worker_task_path(name) %></td>
    <td><%= link_to 'Show Tasks', worker_tasks_path(name) %></td>

  </tr>
<% end %>
</table>

in order to use the link of: new_worker_task_path,

I defined in the task_controller:

def new
    @worker = Worker.find(params[:worker_id])
    @task = @worker.tasks.new
    respond_with(@worker)
end

In addition, I defined: new.html.erb in the views/tasks, that also has: "Hi".

When I pressed the link of: "new task", I got:

Couldn't find Worker with id=alon
Rails.root: /home/alon/projects/TODO

Application Trace | Framework Trace | Full Trace
app/controllers/tasks_controller.rb:48:in `new'
Request

Parameters:

{"worker_id"=>"alon"}

first question: how can I find the worker who I want to add him a task?

second question: as I said, I defined:

<td><%= link_to 'new Task', new_worker_task_path(name) %></td>

why should I have to send the name? I use this value? I don't really understand why this parameter is necessary..

Alon Shmiel
  • 6,753
  • 23
  • 90
  • 138

1 Answers1

1

You have to send actual :param_key, which by default is ID.

So,

new_worker_task_path()

# have to receive worker's ID as argument. Or worker object, accepted too...

new_worker_task_path(@worker)

Updated for the 1st question: Let me guess what you want.

<% @workers.group_by(&:name).each do |name, workers| %>
  <tr>
    <td><%= name %></td>
    <td><%= workers.map {|w| w.tasks.size}.sum %></td>
    <td><%= workers.map {|w| w.tasks.select{ |task| task.done != 'yes' }.size}.sum %></td>
    <td>
      <% workers.each do |worker| %>
        <%= link_to 'new Task', new_worker_task_path(worker) %>
      <% end %>
    </td>
    <td>
      <% workers.each do |worker| %>
        <%= link_to 'Show Tasks', worker_tasks_path(worker) %>
      <% end %>
    </td>
  </tr>
<% end %>
Valery Kvon
  • 4,438
  • 1
  • 20
  • 15
  • thank you for your comment. I understood the differences. what's about the first question? – Alon Shmiel Dec 24 '12 at 02:25
  • For the first question i have to know where '<%= link_to 'new Task', new_worker_task_path(name) %>' has been rendered? controller/action code. Otherwise it is hard to figure out what you expect. – Valery Kvon Dec 24 '12 at 02:37
  • sorry, I forget to say: it's found in "views/workers/index.html.erb". – Alon Shmiel Dec 24 '12 at 02:57
  • 1
    updated. View code you provided a little crazy. :) You send @workers.group_by and expect task in a resulting hash. – Valery Kvon Dec 24 '12 at 03:15
  • first of all, thank you again. I know that it's crazy, but in your code, I see all the tasks of all of the workers. I want to group it. for example, in your code I got: "alon 1 1" "alon 2 1" "Dan 1 1" I want to produce: "alon 3 2", "dan 1 1". – Alon Shmiel Dec 24 '12 at 03:18
  • 1
    look now. But this is weird anyway. Worker is an object. You cannot link_to several objects, only to one. Or modify your application logic. in my code you see grouped workers by name, sum of their tasks and of course as many links as workers are. – Valery Kvon Dec 24 '12 at 03:29
  • wow! thank you! I try to understand your code tomorrow (now: 5:32 AM). so thank you very much!! – Alon Shmiel Dec 24 '12 at 03:31