2016-10-06 48 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 
相關問題