Question

After following the RoR getting started tutorial, I added another model as:

$ rails g model Category name:string wdate:date

I added the line resources :category to my routes.rb file, and copy+pasted the initial Posts controller, changing names from Post to Category and Posts to Categories.

I did similarly with the index, form, new, and edit views.

Now, localhost:3000/category gives me an empty list of categories, and /category/new gives me an input form. Upon submission, however, I get

Routing Error

No route matches [POST] "/categories"

Try running rake routes for more information on available routes

Running rake routes as suggested shows that I have:

POST   /category(.:format)          category#create

Why is the form submitting to the wrong address? The only area where I put in the term "categories" was for the variable name in the #index handler, so I don't understand why the form target is /categories. Do I have to explicitly set the path as /category for creation?

Was it helpful?

Solution

Your resources :category line inside config/routes.rb should be resources :categories.

When you're doing resources, you should always use the pluralized name of the resource so that Rails will automatically singularize it for member routing helpers of that resource, such as edit_category_path(category).

It will then use the plural form in routes for collections, like the form_for. Because the object is new, the form will issue a POST request to /categories with the form data.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top