希望这个文件足以解决问题。一切正常,我无法保存帖子。
路线:
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
答案 0 :(得分:0)
你做&#34; GET&#34;发表#new和a&#34; POST&#34;对于帖子#create。 新操作旨在返回&#34; POST&#34;所需的表单。到创造行动。你不会发布新动作。
答案 1 :(得分:0)
确保您的Post表单代码如下所示:
<%= form_for(@post) do |f| %>