Ruby on rails 获取任务的id:@task=Worker。其中(:name=>;params[:Worker\u id])

Ruby on rails 获取任务的id:@task=Worker。其中(:name=>;params[:Worker\u id]),ruby-on-rails,Ruby On Rails,我有两个控制器:工人和任务 这是任务\u控制器的变量索引: def index @task = Worker.where(:name => params[:worker_id]) respond_to do |format| format.html # show.html.erb format.json { render json: @worker } end end 这是我的任务index.html.erb: <table&g

我有两个控制器:工人和任务

这是任务\u控制器的变量
索引:

def index
   @task = Worker.where(:name => params[:worker_id])

   respond_to do |format|
        format.html # show.html.erb
        format.json { render json: @worker }
    end
end
这是我的任务
index.html.erb

<table>
  <% @task.each do |task| %>
    <tr>
      <td><%= task.name %></td>
      <td><%= task.task %></td>
      <td><%= task.done %></td>
      <td><%= link_to 'Edit', edit_worker_task_path(task.name,1) %></td>
    </tr>
  <% end %>
</table>
这是我的
models/worker.rb

class Worker < ActiveRecord::Base
   attr_accessible :done, :name, :task
end
class Worker
也许你应该这样做

edit_worker_task_path(task, @task)

在阅读源代码时,我注意到您有一些奇怪的命名(@task=Worker.where)?

也许您应该这样做

edit_worker_task_path(task, @task)

在阅读源代码时,我注意到您有一些奇怪的命名(@task=Worker.where)?

要链接到特定任务的编辑页面,只需这样做:

<%= link_to 'Edit', edit_worker_task_path(task) %>

如果这是一个嵌套路由,您可能希望确保有一个编辑任务路由,以便可以执行以下操作:

<%= link_to 'Edit', edit_task_path(task) %>

但是,由于@task变量实际上是一组工人模型,如果调用了edit_task_path(task)helper,那么最终会出现在edit task页面上,但它会传递工人的id。这有意义吗

编辑
聊了一会儿之后,我们创建了必要的任务和工作模型,整理了migrations和routes.rb文件。然后将他指向新的工作者任务路径(工作者)
,并查看工作者索引页。

要链接到特定任务的编辑页,只需执行以下操作:

<%= link_to 'Edit', edit_worker_task_path(task) %>

如果这是一个嵌套路由,您可能希望确保有一个编辑任务路由,以便可以执行以下操作:

<%= link_to 'Edit', edit_task_path(task) %>

但是,由于@task变量实际上是一组工人模型,如果调用了edit_task_path(task)helper,那么最终会出现在edit task页面上,但它会传递工人的id。这有意义吗

编辑
聊了一会儿之后,我们创建了必要的任务和工作模型,整理了migrations和routes.rb文件。然后将他指向
new\u worker\u task\u path(worker)
,并查看worker索引页。

您的链接\u应该像这样使用帮助器:

<%= link_to 'Edit', edit_worker_task_path(id: task.id) %>

您的链接应使用以下帮助器:

<%= link_to 'Edit', edit_worker_task_path(id: task.id) %>

谢谢,但我遇到了这个错误:路由错误没有路由匹配{:action=>“edit”,:controller=>“tasks”,:worker\u id=>#}请尝试运行rake路由以获取有关可用路由的详细信息。可能是因为您使用的是嵌套路由-您有编辑任务路由吗?如果你发布你的模型也会有帮助的。看看你的routes.rb文件,确保你有一个条目添加参考资料:任务在它自己的一行上,然后看看我关于@task/Worker问题的答案。让我们感谢你,但我得到了这个错误:路由错误没有路由匹配{:action=>“edit”,:controller=>“tasks”,:Worker\u id=>#请尝试运行rake路由以获取有关可用路由的更多信息。可能是因为您使用的是嵌套路由-您是否有编辑任务路由?如果你发布模型也会有所帮助。请查看routes.rb文件,确保你有相应的条目添加参考资料:任务在自己的一行上,然后查看我关于@task/Worker问题的回答。让我们看看我有一个错误。它将指引我到:。此外,错误是:找不到名为的Worker=#我遇到了一个错误。它将指引我到:。此外,错误是:找不到名为的Worker=#不幸的是,我得到了错误:TasksController#edit uninitialized constant TasksController::Task Rails.root:/home/alon/projects/TODO Application Trace | Framework Trace | Full Trace app/controllers/tasks\u controller.rb:21:in`edit'请求参数:{“Worker\u id=>“alon”,“id”=>“1”}不幸的是,我得到了错误:TasksController#edit uninitialized constant TasksController::Task Rails.root:/home/alon/projects/TODO应用程序跟踪| Framework Trace | Full Trace app/controllers/tasks\u controller.rb:21:`edit'请求参数中:{“worker\u id”=>“alon”,“id”=>“1”}