0

我有两个控制器:任务,任务人员。

我有意见/任务/index.html.erb:

<table>
    <% @tasks.group_by(&:name).each do |name, tasks| %>
    <tr>
      <td><%= name %></td>
      <td><%= tasks.size %></td>
      <td><%= tasks.select{ |task| task.done != true }.size %></td>
    </tr>
    <% end %>
</table>

我想在views/tasks/index.html中创建一个到views/tasksperson/index.html.erb的链接。我还想将名称发送到Tasksperson_controller中的“索引”中。我试图通过获取参数来做到这一点[: name] 但我认为这是错误的

也许,我需要做类似的事情:

<td><%= link_to 'Show Tasks', tasksperson_path(name) %></td>

这是我的任务person_controller:

class TaskspersonController < ApplicationController
    def index
       @tasks = Task.where(:name => params[:name]) respond_to do |format|
          format.html # index.html.erb
          format.json { render json: @tasks }
       end
    end
end

和意见/tasksperson/index.html.erb:

<table>
  <tr>
    <th>Name</th>
    <th>num of tasks</th>
    <th>num tasks left</th>
    <th>test</th>
  </tr>

  <% @tasks.each do |f| %>
  <tr>
    <td><%= f.name %></td>
    <td><%= f.task %></td>
    <td><%= f.done %></td>
  </tr>
  <% end %>
</table>
4

2 回答 2

1

根据您的评论“ ......所以任务有很多任务人员”我认为您想要一个类似于下面的数据模型

class Task < ActiveRecord::Base
  has_many :assigned_tasks
  has_many :people, :through => :assigned_tasks
end

# maybe this is just the User class?
class Person < ActiveRecord::Base 
  has_many :assigned_tasks
  has_many :tasks, :through => :assigned_tasks
end

# was TaskPerson
class AssignedTask < ActiveRecord::Base
  belongs_to :task
  belongs_to :person
end

有关“has_many :through Association”的信息,请参阅http://guides.rubyonrails.org/association_basics.html#the-has_many-through-association

task = Task.create(:title => "Go up the hill")
jack = Person.find(00000)
jill = Person.find(00000)

task.people << jack
task.people << jill

task.assigned_tasks.each do |join|
  puts join.created_at
  puts join.person.name
  # 0 - jack
  # 1 - jill
end

task.people.each do |person|
  puts person.name
end

我不确定您要在视图中显示什么,看起来您正在按任务/索引中的任务名称属性进行分组,那是人名吗?

于 2012-12-22T19:15:47.683 回答
1

您需要将:name作为参数添加到定义路由的规则中TaskspersonController#indexroutes.rb 因此它将是这样的:

match 'tasksperson/index/:name' => 'tasksperson#index', as: :tasksperson_path

于 2012-12-22T18:07:33.093 回答