2016-10-06 46 views
2

我新的轨道,我得到这个错误:未定义的方法`posts_path” <#<类别:0x007fe3547d97d8>:0x007fe3546d58f0>

undefined method `posts_path' for #<#<Class:0x007fe3547d97d8>:0x007fe3546d58f0> 

我下面张贴了我的文件,请请记住,我是新来的铁轨,这样简单的解释将非常感激!

Route.rb:

Rails.application.routes.draw do 
    get '/post' => 'post#index' 
    get '/post/new' => 'post#new' 
    post 'post' => 'post#create' 
end 

post_controller.rb:

class PostController < ApplicationController 
    def index 
     @post = Post.all 
    end 

    def new 
     @post = Post.new 
    end 

    def create 
     @post = Post.new(post_params) 
     if @post.save 
     redirect_to '/post' 
     else 
     render 'new' 
     end 
    end 

    private 
    def post_params 
     params.require(:post).permit(:content).permit(:title) 
    end 
end 

new.html.erb:

<%= form_for(@post) do |f| %> 
    <div class="field"> 
    <%= f.label :post %><br> 
    <%= f.text_area :title %> 
    <%= f.text_area :content %> 
    </div> 
    <div class="actions"> 
    <%= f.submit "Create" %> 
    </div> 
<% end %> 

回答

4

我猜form_for(@post)希望那里是一个方法称为posts_path,其中一个不存在,因为它尚未在您的路线文件中定义。尝试更换:

Rails.application.routes.draw do 
    get '/post' => 'post#index' 
    get '/post/new' => 'post#new' 
    post 'post' => 'post#create' 
end 

Rails.application.routes.draw do 
    resources :posts, only: [:new, :create, :index] 
end 

编辑:更多的信息:

http://guides.rubyonrails.org/form_helpers.html阅读形式助手完整的页面,并inparticular,读取绑定表对“2.2节一个对象“,并说:

When dealing with RESTful resources, calls to form_for can get significantly easier if you rely on record identification. In short, you can just pass the model instance and have Rails figure out model name and the rest:

## Creating a new article 
# long-style: 
form_for(@article, url: articles_path) 
# same thing, short-style (record identification gets used): 
form_for(@article) 

## Editing an existing article 
# long-style: 
form_for(@article, url: article_path(@article), html: {method: "patch"}) 
# short-style: 
form_for(@article) 

Notice how the short-style form_for invocation is conveniently the same, regardless of the record being new or existing. Record identification is smart enough to figure out if the record is new by asking record.new_record?. It also selects the correct path to submit to and the name based on the class of the object.

所以,明知或不知道,当你说form_for(@post),根据您的变量@post的名称,您要求铁轨猜测您的表单应该提交给的路线。您定义的路线与导轨预期的路线不符。

欲了解更多关于在rails中路由的信息,请阅读http://guides.rubyonrails.org/routing.html的整个页面,特别要注意“2资源路由:Rails默认值”部分。你的form_for(@post)会假设你正在使用“资源路由”,这是我转向。

至于为什么你得到一个新的错误?在您的应用程序中还有其他位置,您期望使用之前定制的自定义路线,而现在您正在使用导轨“资源路线”,因此您的路径名称会有所不同。没有路由匹配[GET]“/ post/new”,因为现在路由匹配没有路由匹配[GET]“/ posts/new”(注意复数帖子)。

+0

或者通过路由的名称为'后“后” =>“后#创建”为:posts' – AbM

+0

为什么会想到一个名为posts_path方法?你公布的代码如何解决这个问题?感谢您的答案,并抱歉noob问题=) – user11406

+0

此外,现在我得到,“没有路线匹配[GET]”/ post/new“” – user11406

0

这里的表单试图通过路径“posts_path” 找到post_method的路由所以你需要在你的routes.rb文件中定义。

Rails.application.routes.draw do 
get '/post' => 'post#index' 
get '/post/new' => 'post#new' 
post '/posts' => 'post#create' 
end 
相关问题