我无法保存 Post。没有路线匹配 [POST] “/posts/new”
I can't save the Post. No route matches [POST] "/posts/new"
希望这些文件足以解决问题。一切正常我只是无法保存 post.
路线:
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
你为 post#new 做了 "GET",为 post#create 做了 "POST"。
新操作旨在 return 所需的表单 "POST" 创建操作。你没有post新动作。
确保您的 Post 表单代码像这样开始:
<%= form_for(@post) do |f| %>
希望这些文件足以解决问题。一切正常我只是无法保存 post.
路线:
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
你为 post#new 做了 "GET",为 post#create 做了 "POST"。 新操作旨在 return 所需的表单 "POST" 创建操作。你没有post新动作。
确保您的 Post 表单代码像这样开始:
<%= form_for(@post) do |f| %>