と の 2 つのコントローラーがtasks
ありworkers
ます。
訪問しまし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.erb
edit.html.erb
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 %>