The Whiz of Oz
The Whiz of Oz

Reputation: 7043

Rails 4 Routing Error - no rout matches [POST] "/categories/new"

While learning Rails 4 I stuck while trying to add categories to posts of my simple blog. I have generated model, ran the migration and added a controller. No matter what I do now when trying to create a category, I keep running into same mistake: no route matches [POST], which is weird, as I seem to have all the code in place. Please help!

categories controller

class CategoriesController < ApplicationController

    def index
      @categories = Category.all
    end

    def new
      @category = Category.new
    end

    def create
      @category = Category.new(category_params)

        @category.save
        redirect_to new_category_path, alert: "Category created!"
    end

    def show
      @category = Category.find(params[:id])
    end

    def destroy
      @category = Category.find(params[:id])
      @category.destroy

      redirect_to categories_path
    end


  private
        def category_params
            params.require(:category).permit(:name)
        end

end

routes.rb

Blog::Application.routes.draw do
  get 'tags/:tag', to: 'posts#index', as: :tag

  resources :categories

  resources :posts do
    resources :comments
  end

  root 'welcome#index'
end

category.rb

class Category < ActiveRecord::Base
    validates :name, presence: true
    has_many :posts
end

new.html.erb

<%= form_for :category do |f| %>
  <p>
    <%= f.label :name %><br>
    <%= f.text_field :name %>
  </p>

  <p>
    <%= f.submit %>
  </p>
<% end %>

/categories/new

No route matches [POST] "/categories/new"

Upvotes: 1

Views: 1549

Answers (2)

Leo Correa
Leo Correa

Reputation: 19789

Although the @category works, if you read a bit further you will see that they will explain why the your code sends No route matches [POST] "/categories/new".

The guide actually explains that you need to specify the url: posts_path for the form to use the right route.

There's one problem with this form though. If you inspect the HTML that is generated, by viewing the source of the page, you will see that the action attribute for the form is pointing at /posts/new. This is a problem because this route goes to the very page that you're on right at the moment, and that route should only be used to display the form for a new post.

The form needs to use a different URL in order to go somewhere else. This can be done quite simply with the :url option of form_for. Typically in Rails, the action that is used for new form submissions like this is called "create", and so the form should be pointed to that action.

Edit the form_for line inside app/views/posts/new.html.erb to look like this:

<%= form_for :post, url: posts_path do |f| %> 

In this example, the posts_path helper is passed to the :url option. What Rails will do with this is that it will point the form to the create action of the current controller, the PostsController, and will send a POST request to that route.

Upvotes: 1

Marek Lipka
Marek Lipka

Reputation: 51151

You should have in your view

<%= form_for @category do |f| %>
  <p>
    <%= f.label :name %><br>
    <%= f.text_field :name %>
  </p>

  <p>
    <%= f.submit %>
  </p>
<% end %>

@category object is used by form_for method to figure out form url.

If you pass only Symbol to form_for method, without specifying url explicitly, form will be generated with url being the current url.

Upvotes: 2

Related Questions