4

I am absolutely and totally new to rails, so the answer is probably very simple. Here goes:

My page is generating this error

NoMethodError in Tasks#new
Showing app/views/tasks/new.erb where line #3 raised:

undefined method `tasks_path' for #

Here is the view:

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

    <%= f.label :description %>:
    <%= f.text_field :description %><br />

    <%= f.label :priority %>:
    <%= collection_select(:news, :priority_id, Priority.find(:all), :id, :description) %><br />

    <%= f.submit "Add Task" %>

<% end %>

The controller:

class TasksController < ApplicationController
    def index
        @all_tasks = Task.find(:all, :order => :id)
    end

    def new
        @task = Task.new
    end ...(more)

and the model:

I can't see a problem, but like I said, I'm clueless so far. Thanks!

class Task < ActiveRecord::Base
    validates_presence_of :description

    belongs_to :priority
    has_and_belongs_to_many :staff
    has_and_belongs_to_many :catagory

end
doctororange
  • 11,670
  • 12
  • 42
  • 58

5 Answers5

5

Do you have

map.resources :tasks

in your routes?

Jarrod
  • 2,398
  • 16
  • 14
  • Oh, you're right on. After adding that, I was able to use form_for with @task – doctororange May 10 '09 at 13:36
  • If you've got @task = Task.new in your action, the form_for(@task) references the empty task object. :task would make your form submit to tasks/new instead of a RESTful task/ with method='post'. – Jarrod May 10 '09 at 13:39
4

Thanks for the answers.

As predicted, as simple problem.

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

should be:

<% form_for(:task) do |f| %>

Funny how you always find the answer to a question right after you post it! Thanks again.

doctororange
  • 11,670
  • 12
  • 42
  • 58
1

Please check your file name in View.. It should have extension .html.erb not only .erb ...

Mihai Iorga
  • 39,330
  • 16
  • 106
  • 107
Abhijit Muke
  • 1,194
  • 3
  • 16
  • 41
1

Regarding this code:

@all_tasks = Task.find(:all, :order => :id)

You don't need to specify order by id because it's the default behavior. So this should suffice.

@all_tasks = Task.find(:all)

And this can be further condensed to the following

@all_tasks = Task.find.all

Furthermore, a rails convention is to name your instance variable @tasks

@tasks = Task.find.all

Have fun with Rails.

allesklar
  • 9,506
  • 6
  • 36
  • 53
  • Thanks. Task.find.all gives the error "Couldn't find Task without an ID" ~$ ruby --version ruby 1.8.7 (2008-08-11 patchlevel 72) [i486-linux] ~$ rails --version Rails 2.3.2 – doctororange May 18 '09 at 15:28
0

Have you generated this example using the scaffold generator? Because if not it might be that you forgot to define the ressource url mapping in the routes.rb:

--- a/config/routes.rb
+++ b/config/routes.rb
@@ -1,4 +1,6 @@
 ActionController::Routing::Routes.draw do |map|
+  map.resources :tasks
+

Don't forget to restart webrick after you've added the route!

reto
  • 16,189
  • 7
  • 53
  • 67
  • Also checkout 'http://www.railsbrain.com/api/rails-2.3.2/doc/index.html?a=M000295&name=resources' Hmm.. and @task should work. – reto May 10 '09 at 13:35