我有两个控制器:任务,任务员。
我有views / tasks / 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中将名称发送到'index'。我试着做通过获取params [:name],但我认为这是错误的
也许,我需要做类似的事情:
<td><%= link_to 'Show Tasks', tasksperson_path(name) %></td>
这是我的tasksperson_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
和views / 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>
答案 0 :(得分:1)
您需要将:name
作为参数添加到规则中,该规则定义TaskspersonController#index
中routes.rb
的路由
所以它会是这样的:
match 'tasksperson/index/:name' => 'tasksperson#index', as: :tasksperson_path
答案 1 :(得分:1)
根据你的评论&#34; ...所以任务有很多任务人员&#34;我想你想要一个类似于下面的数据模型
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
有关&#34; has_many:通过协会&#34;
的信息,请参阅http://guides.rubyonrails.org/association_basics.html#the-has_many-through-associationtask = 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
我不确定你要在你的视图中显示什么,看起来你正在按任务/索引中的任务名称属性进行分组,那是人名吗?