2017-03-09 51 views
0

希望这个文件足以解决问题。所有工作我都无法保存这篇文章。我无法保存帖子。没有路线匹配[POST]“/ posts/new”

路线:

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

请从“回扣路线” – David

+0

发送输出'new'应该是'GET'请求而不是职位(POST请求)。 – fanta

+0

前缀动词URI模式控制器#动作 root GET/posts#index posts GET /posts(.:format)posts#index POST /posts(.:format)posts#create new_post GET/posts/new(.:格式)posts#new edit_post GET /posts/:id/edit(.:format)posts#edit post GET /posts/:id(.:format)posts#show –

回答

0

确保您的Post表单代码开头是这样的:

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

<%= form_for:post do | f | %> –

+0

将其更改为<%= form_for(@post)do | f | %>,看看会发生什么。表单助手以实例变量的名称 - 这里“@post”作为参数..阅读更多关于表单助手在这里:http://guides.rubyonrails.org/form_helpers.html –

+0

很高兴帮助!请将答案标记为已接受:) –

0

你 “GET” 为岗位#新的, “POST” 的帖子#创建。 新操作旨在将“POST”所需的表单返回到创建操作。您不会发布到新操作。

+0

我是ROR的新手。我该如何解决它? –

+0

你打算如何发布新信息?你应该有一个这样的链接:new_post_path –

+0

我在index.html.erb –