2016-10-06 7 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と呼ばれ、ルートファイルに定義されていないため存在しません。 http://guides.rubyonrails.org/form_helpers.htmlでフォームヘルパーの全ページを読む

、および特に、フォームへのバインド2.2」のセクションをお読みください。

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 

で:より多くの情報を交換してみてくださいオブジェクト」と言っている部分:

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変数の名前に基づいて、フォームに送信するルートを推測するように指示しています。あなたが定義したルートは、レールが期待していたものと一致しませんでした。

レールでのルーティングの詳細については、ページ全体をhttp://guides.rubyonrails.org/routing.htmlで読んでください。詳細については、「2リソースルーティング:レールのデフォルト」を参照してください。 form_for(@post)は、あなたが「リソースルーティング」を使用していると仮定します。これは、私が切り替えたものです。

新しいエラーが表示される理由は?あなたのアプリケーションには、以前定義したカスタムルートを使用することを期待していた場所があり、パスの名前が異なるように "リソースルート"を使用しています。 [GET]/posts/new(複数の投稿に注意してください)と一致するルートがないため、[GET] "/ post/new"と一致するルートはありません。

+0

や 'ポスト「ポスト」=>のようにルートの名前を渡すと「ポスト#作成」:posts' – AbM

+0

なぜそれがposts_pathというメソッドを期待していますか?そして、あなたが投稿したコードはそれをどのように解決しますか?答えをお寄せいただきありがとうございます。また、noobの質問でごめんなさい=) – user11406

+0

また、「経路は[GET]と一致しません」/投稿/新着 " – user11406

0

ここでフォームはpost_methodへの経路を "posts_path"というパスで見つけようとしています したがって、routes.rbファイルで定義する必要があります。

Rails.application.routes.draw do 
get '/post' => 'post#index' 
get '/post/new' => 'post#new' 
post '/posts' => 'post#create' 
end 
関連する問題