Rails Error "NoMethodError" - My First Ruby Application

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

My page generates this error

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

 undefined method `tasks_path 'for #

Here is a 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 %> 

Controller:

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

and model:

I do not see a problem, but, as I said, I still do not know. 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 
+4
source share
5 answers

You have

 map.resources :tasks 

in your routes?

+5
source

Thanks for answers.

As predicted, a simple problem.

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

it should be:

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

It's funny how you always find the answer to a question right after its publication! Thanks again.

+4
source

Regarding this code:

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

You do not need to specify the order by id, because this is the default behavior. Therefore, this should be enough.

 @all_tasks = Task.find(:all) 

And this can be reduced to the following

 @all_tasks = Task.find.all 

Also, the rails convention should call your @tasks instance variable

 @tasks = Task.find.all 

Have fun with Rails.

+1
source

Please check the name of your file in the view. It must have the extension .html.erb not only .erb ...

+1
source

Have you generated this example with the scaffold generator? Because if not, you may have forgotten to define the mapping of the ressource URL to route.rb:

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

Remember to restart webrick after you add the route!

0
source

All Articles