user393964
user393964

Reputation:

Undefined method *_path when trying to use form_for

Category controller:

  def new
    @cat = Category.new
    respond_to do |format|
      format.html
    end
  end

View:

%p Add new category:
~form_for(@cat) do |f|
  %div.field
    ~f.label :name
    ~f.text_field :name
  %div.field
    ~f.label :description
    ~f.text_area :description
  %div.field
    ~f.submit

Routes:

 resources :category 

When I try to load category/new in the browser I get:

undefined method `categories_path' for #<#<Class:0x10d9c9ee8>:0x10d9b0768>
Extracted source (around line #3):
1: %h1 Category#new
2: %p Add new category:
3: ~form_for(@cat) do |f|
4:   %div.field
5:     ~f.label :name

Any ideas why my form isn't showing? Also, on my category/index page, where I want to show all categories, under the list of categories I'm getting #<Category:0x10d736b40>. Can I get rid of that somehow?

Upvotes: 0

Views: 156

Answers (2)

mind.blank
mind.blank

Reputation: 4880

A bit long for a comment so I've added the following as an answer instead.

If you want a singular resource you need to do:

resource :category

Which will generate only 6 routes (no index):

GET     /category/new   new
POST    /category       create
GET     /category       show
GET     /category/edit  edit
PUT     /category       update
DELETE  /category       destroy

But your controller will still be plural, unless you do the following:

resource :category, controller: :category

Upvotes: 0

Simone Carletti
Simone Carletti

Reputation: 176552

The route should be

resources :categories

not

resources :category

Upvotes: 1

Related Questions