访问索引页上的链接模型的属性。

时间:2022-09-03 07:17:02

I have an index page showing all tasks in rails. A task belongs_to a status. And a status has_many tasks. I have set that correctly in the task and status models.

我有一个显示rails所有任务的索引页面。任务归属于状态。一个状态有很多任务。我已经在任务和状态模型中正确设置了。

In the task controller I have @tasks = Task.find(:all)

在任务控制器中,我有@tasks =任务。find(:all)

In the index.html.erb for the task controller I can see all the fields of the tasks but i cannot access the status field of a task.

在index . html。对于任务控制器的erb,我可以看到任务的所有字段,但我不能访问任务的状态字段。

How can I do this? What code should I put in the task controller index action and in the index.html.erb template of the task controller? Please help.

我该怎么做呢?我应该在任务控制器索引操作和index.html中放入什么代码。任务控制器的erb模板?请帮助。

i now have something like

我现在有一些类似的东西。

tasks.each do 
  task.name 
end

Here I want to put the accessed status of a task!

在这里,我想把一个任务的访问状态放在这里!

Thanks

谢谢

1 个解决方案

#1


1  

To pull in all tasks with status in your controller, do:

要在控制器中执行所有任务,请执行以下操作:

@tasks = Task.all(:include => status)

By status, you need to use the plural form of that word, however that is defined in your system.

通过状态,您需要使用该词的复数形式,然而,这是在您的系统中定义的。

To access the status of each task in the view, do:

要访问视图中的每个任务的状态,请执行以下操作:

tasks.each do |task|
  task.name
  task.status.name (or whichever field of status you want)
end

#1


1  

To pull in all tasks with status in your controller, do:

要在控制器中执行所有任务,请执行以下操作:

@tasks = Task.all(:include => status)

By status, you need to use the plural form of that word, however that is defined in your system.

通过状态,您需要使用该词的复数形式,然而,这是在您的系统中定义的。

To access the status of each task in the view, do:

要访问视图中的每个任务的状态,请执行以下操作:

tasks.each do |task|
  task.name
  task.status.name (or whichever field of status you want)
end