Search code examples
ruby-on-railsrubyblogs

I can't save the Post. No route matches [POST] "/posts/new"


Hope that this files is enough to solve the problem. All is working I just can't save the post.

routes:

Rails.application.routes.draw do
  root 'posts#index'
  resources :posts
end

post_controller:

class PostsController < ApplicationController

  def index
    @posts = Post.all
  end

  def show
    @post = Post.find(params[:id])
  end

  def new
    @post = Post.new
  end

  def create
    @post = Post.new(post_params)
    if @post.save
      flash[:notice] = "Successfully created post!"
      redirect_to post_path(@post)
    else
      flash[:alert] = "Error creating new post!"
      render :new
    end
  end

  private

  def post_params
    params.require(:post).permit(:author, :title, :summary, :body)
  end

end

Solution

  • Make sure your Post form code starts like this:

    <%= form_for(@post) do |f| %>