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 %>

理由はわかりません。私は、favorite_relationships_controller.rb という名前のコントローラーと、コードを含むモデル ファイル favorite_relationship.rb を持っています。

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

私のユーザーモデルには次のものもあります:

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

なぜそのエラーが発生するのか本当にわかりません。助けていただければ幸いです。

4

2 に答える 2

2

Rails には、 で設定されたルート用の_pathおよびヘルパーがあります。のルートを定義したことを確認する必要があります。何かのようなもの:_urlconfig/routes.rbFavouriteRelationshipController

resources :favourite_relationships

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

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

于 2012-04-28T22:48:30.783 に答える
2

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

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

MyApp::Application.routes.draw do
  resources :favourite_relationships
end

これにより、RESTful リソースに必要な典型的な「CRUD」ルートが生成されます。

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"}
于 2012-04-28T22:47:27.723 に答える