0

我有两个控制器:tasksworkers.

我参观了localhost:3000/workers。当我按下“添加新任务”(这是我放的链接:)<%= link_to 'Add a New Task', new_worker_path %>时,出现错误:

NoMethodError in Workers#new

Showing /home/alon/projects/todo/app/views/workers/new.html.erb where line #3 raised:

undefined method `tasks_path' for #<#<Class:0xb663ccf0>:0xb663b3c8>
Extracted source (around line #3):

1: <h1>New task</h1>
2: 
3: <%= form_for @workers do |f| %>
4:  <p>
5:      <%= f.label :name %><br />
6:      <%= f.text_field :name %>
Rails.root: /home/alon/projects/todo

Application Trace | Framework Trace | Full Trace
app/views/workers/new.html.erb:3:in     `_app_views_workers_new_html_erb___132490529__619346358'
app/controllers/workers_controller.rb:28:in `new'
Request

Parameters:

None
Show session dump

Show env dump

Response

Headers:

None

为什么我会收到这些错误?

我创建了文件_form.html.erb, index.html.erb, new.html.erb,show.html.erbedit.html.erbin views/workers

我在中定义routes.rb

resources :workers do
    resources :tasks
end

这是我的workers_controller

class WorkersController < ApplicationController

  def index
    @workers = Task.all

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @workers }
    end
  end

  # GET /workers/1
  # GET /workers/1.json
  def show
    #.find(params[:id])
     @workers = Task.find(params[:id])
    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @workers }
    end
  end

  # GET /workers/new
  # GET /workers/new.json
  def new
    @workers = Task.new

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @workers }
    end
  end

  # GET /workers/1/edit
  def edit
    @worker = Task.find(params[:id])
  end

  # POST /workers
  # POST /workers.json
  def create
    @worker = Task.new(params[:task])

    respond_to do |format|
      if @worker.save
        format.html { redirect_to @worker, notice: 'Your task was created.' }
        format.json { render json: @worker, status: :created, location: @worker }
      else
        render "new"
      end
    end
  end

  # PUT /workers/1
  # PUT /workers/1.json
  def update
    @worker = Task.find(params[:id])

    respond_to do |format|
      if @worker.update_attributes(params[:task])
        format.html { redirect_to @worker, notice: 'Task was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: "edit" }
        format.json { render json: @worker.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /workers/1
  # DELETE /workers/1.json
  def destroy
    @worker = Task.find(params[:id])
    @worker.destroy

    respond_to do |format|
      format.html { redirect_to tasks_url }
      format.json { head :no_content }
    end
  end

end

另外,我在以下代码中编写了models/tasks.rb

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

这是我的workers/new.html.erb文件:

<h1>New task</h1>

<%= form_for @workers do |f| %>
<p>
    <%= f.label :name %><br />
    <%= f.text_field :name %>
</p>
<p>
    <%= f.label :task %><br />
    <%= f.text_area :task %>
</p>
<p>
    <%= f.label :done %><br />
    <%= f.check_box :done %>    
</p>
<p>
    <%= f.submit "add a new task" %>
</p>
<% end %>

这是workers/_form.html.erb

<%= form_for(@task) do |f| %>

  <% if @task.errors.any? %>
      <div id="error_explanation">
          <h2><%= pluralize(@task.errors.count, "error") %> prohibited this task from being saved:</h2>
          <ul>
              <% @task.errors.full_messages.each do |msg| %>
                 <li><%= msg %></li>
              <% end %>
          </ul>
      </div>
  <% end %>

  <div class="field">
    <%= f.label :name %><br />
    <%= f.text_field :name %>
  </div>
  <div class="field">
    <%= f.label :task %><br />
    <%= f.text_area :task %>
  </div>
  <div class="field">
    <%= f.label :done %><br />
    <%= f.check_box :done %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>
4

2 回答 2

2

如果您输入 rake routes,您将看到:

$ rake routes
    worker_tasks GET    /workers/:worker_id/tasks(.:format)          tasks#index
                 POST   /workers/:worker_id/tasks(.:format)          tasks#create
 new_worker_task GET    /workers/:worker_id/tasks/new(.:format)      tasks#new
...

因此,当您new_worker_task工人传给.

<%= link_to 'Add a New Task', new_worker_task_path(worker) %> # (below)

我在本地重新创建了您的应用程序并使其可以使用:

<table>
  <tr>
    <th></th>
    <th></th>
    <th></th>
  </tr>

<% @workers.each do |worker| %>
  <tr>
    <td><%= link_to 'Show', worker %></td>
    <td><%= link_to worker.name, worker %></td>
    <td><%= link_to 'Edit', edit_worker_path(worker) %></td>
    <td><%= link_to 'New Task', new_worker_task_path(worker) %>
    <td><%= link_to 'Destroy', worker, method: :delete, data: { confirm: 'Are you sure?' } %></td>
  </tr>
<% end %>
</table>

请注意,我为工作人员添加了一个“名称”属性,以使其更易于显示(将其添加到表单和索引页面,当然还有一个数据库迁移以将其添加到数据库中)。

正如 pjammer 指出的那样,您也可以通过nested_attributes 在表单中执行此操作,例如form_for([@worker, @task])

于 2012-12-23T03:25:39.467 回答
1

您似乎正在使用嵌套路由,但正在传入Task.new而不是类似的东西Worker.new,如果您想向工作人员添加任务,请使用accepts_nested_attributes_for或创建表单对象来处理任务的创建。

于 2012-12-23T03:11:45.583 回答