2011-10-28 24 views
1

I持って次未定義のメソッド `relationships_pathは」レール3.1

ルート:

. 
. 
. 
resources :users do 
    resources :relationships 
end 

new.html.erb:

<section id="main"> 

    <%= form_for @relationship do |f| %> #This is the line the error is on 
    <div class="field"> 
     <%= f.label :name %> 
     <%= f.text_field :name %> 
    </div> 
    <div class="actions"><%= f.submit %></div> 
    <% end %> 
</section> 

relationships_controller.rb

class RelationshipsController < ApplicationController 

    def new 
     @id = params[:user_id] 
     @rel_user = User.find_by_id(params[:user_id]) 
     @relationship = Relationship.new 
    end 

    def create 

    end 

end 

relationship.rb #model

class Relationship < ActiveRecord::Base 
    belongs_to :user 

    # TYPES = ['Family', 'Friend', 'Spouse'] 
end 

私はスタックオーバーフローについて悩んでおり、答えを見つけられないようですが、自分のリソースを入れ子にすることと関係があります。

undefined method 'relationships_path' for #<#<Class:0x007ff45f15ff80>:0x007ff45f15bc78>

任意のアイデア:私は次のエラーを取得しますか?

+1

あなたは、経路変更して 'relationship' ressourcesを入れ子になってきました。あなたの前の質問に従って、 'rake routes'を実行して解決策を得てください。 – apneadiving

+0

@apneadivingなので、私はnew_user_relationshipを使う必要があるのを見ましたが、この行の問題ではありません:私のコントローラの '@relationship = Relationship.new'ですか?私は何が欠けていますか? –

答えて

5

route.rbファイルから生成されたすべての '_path'ヘルパーを理解する必要があります。だからあなたの場合ルートはこのヘルパーを生成しますusers_relationship_pathショーアクションのために。

しかし、あなたの形であなただけの relationship_pathヘルパーを使用することが期待されている@Relationshipのform_forを使用しています。

だから、このように、ネストを使用するようにフォームヘルパーを教えてください:

<%= form_for [@rel_user, @relationship] do |f| %> 
+0

他のすべてがあったのでユーザーを複数にしましたが、ユーザーが多くの関係を持っている場合は単数でなければならないと言っていますか? –

+1

それはあなたが持っているユーザーの数によって異なりますが、私は自分の答えを修正したので正しいかもしれません。 – megas

+0

フォームに渡すには、ユーザーとリレーションシップのインスタンスが必要ですか?あるいはなぜ@current_userではなく@rel_userを使うのですか?私はそれらをテストし、両方が動作するようです。 –

関連する問題