0

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
Ursus
  • 29,643
  • 3
  • 33
  • 50
  • Please send output from "rake routes" – David Mar 09 '17 at 18:40
  • `new` is supposed to be for `get` requests not posts(POST request). – fanta Mar 09 '17 at 18:45
  • Prefix Verb URI Pattern Controller#Action root GET / posts#index posts GET /posts(.:format) posts#index POST /posts(.:format) posts#create new_post GET /posts/new(.:format) posts#new edit_post GET /posts/:id/edit(.:format) posts#edit post GET /posts/:id(.:format) posts#show – Артем Неймышев Mar 09 '17 at 19:12

2 Answers2

0

You do "GET" for post#new and a "POST" for post#create. The new action is designed to return the form required to "POST" to the create action. You don't post to the new action.

Alexander Luna
  • 5,261
  • 4
  • 30
  • 36
0

Make sure your Post form code starts like this:

<%= form_for(@post) do |f| %>
Tomasz Bubała
  • 2,093
  • 1
  • 11
  • 18