2012-04-28 16 views
0

私はこのフォームを表示するときレールで未定義のメソッド

undefined method `favorite_relationships_path' 

エラーが表示されます。

<%= form_for(current_user.favorite_relationships.build(lesson_id: @lesson.id), 
      remote: true) do |f| %> 
    <div><%= f.hidden_field :lesson_id %></div> 
    <%= f.submit "Favorite", class: "btn btn-large btn-primary" %> 
<% end %> 

を私がなぜわかりません。私は本当に、なぜイムことになっわからない

has_many :favorite_relationships 
    has_many :lessons, :through => :favorite_relationships 

:私は私のユーザモデルもあり

class FavoriteRelationship < ActiveRecord::Base 
    attr_accessible :lesson_id 
    belongs_to :user 
    belongs_to :lesson 
end 

favorite_relationships_controller.rbとモデルファイルと呼ばれるコントローラを持って、favorite_relationship.rb、コード付きエラー。ヘルプをいただければ幸いです。

答えて

2

コントローラ、アクション、ビューの定義では不十分です。あなたのコントローラ/アクションにURLを接続するにはconfig/routes.rbに定義する必要があります。あなたのルーティングファイルにresources :favourite_relationshipsというRESTfulリソースを定義することは、Railsが*_path*_urlヘルパーを生成する原因です。これを行うまでリクエストがあなたのアプリに到達する方法はなく、あなたのアプリがあなたのモデルに基づいてルートを生成する方法はありません。

あなたのルートファイルは次のようになります。

これは、RESTfulなリソースに必要な典型的な "CRUD" 路線を生成
MyApp::Application.routes.draw do 
    resources :favourite_relationships 
end 

favourite_relationships  GET /favourite_relationships(.:format)   {:action=>"index", :controller=>"favourite_relationships"} 
          POST /favourite_relationships(.:format)   {:action=>"create", :controller=>"favourite_relationships"} 
new_favourite_relationship GET /favourite_relationships/new(.:format)  {:action=>"new", :controller=>"favourite_relationships"} 
edit_favourite_relationship GET /favourite_relationships/:id/edit(.:format) {:action=>"edit", :controller=>"favourite_relationships"} 
    favourite_relationship GET /favourite_relationships/:id(.:format)  {:action=>"show", :controller=>"favourite_relationships"} 
          PUT /favourite_relationships/:id(.:format)  {:action=>"update", :controller=>"favourite_relationships"} 
          DELETE /favourite_relationships/:id(.:format)  {:action=>"destroy", :controller=>"favourite_relationships"} 
+0

ああ、私たちは同時にほぼ同じ回答を書いています! :) – tiredpixel

+0

ああダング、私はそれについて完全に忘れてしまった。私は自分のルートファイルのURLを考えていただけです。つまり、localhost:3000/lessons/favoritesのようなものの下にあるようなものでした。どうもありがとう! – Sasha

2

Railsはルートの_path_urlヘルパーを持っているが、 config/routes.rbに設定されています。 FavouriteRelationshipControllerのルートを定義していることを確認する必要があります。以下のようなもの:

resources :favourite_relationships 

あなたがrake routesコマンドを使用して、アプリケーション用に定義されたルートを確認することができます。

ルーティングの詳細については、Rails Routing from the Outside Inガイドをご覧ください。

+0

ああダング、私はそれについて完全に忘れてしまった。私は自分のルートファイルのURLを考えていただけです。つまり、localhost:3000/lessons/favoritesのようなものの下にあるようなものでした。どうもありがとう! – Sasha

関連する問題