2011-10-28 226 views
1

我有以下:未定义的方法`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#模型

class Relationship < ActiveRecord::Base 
    belongs_to :user 

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

我已经在Stack Overflow上寻找了,并且似乎找不到答案,但我认为它与嵌套我的资源有关。我得到以下错误:

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

任何想法?

+1

你嵌套在'relationship' ressources这样的路径变化。根据你以前的问题,运行'rake routes'来获得解决方案。 – apneadiving

+0

@apneadiving所以我看到我需要使用:new_user_relationship,但不是这一行的问题:'@relationship = Relationship.new'在我的控制器?我错过了什么? –

回答

5

您应该了解所有'_path'助手从route.rb文件生成的。所以在你的案例路线将产生这个帮手users_relationship_path显示操作。

但在你的表格你只用的form_for @relationship预计将使用relationship_path帮手。

所以,你应该告诉你的表单辅助使用嵌套,就像这样:

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

我使用户复数,因为一切都是,但你是说如果用户有很多关系,那么它应该是单数? –

+1

这取决于你有多少用户,也许你是对的,所以我修复了我的答案。 – megas

+0

你是否只需要一个用户和关系的实例传入表单?或者为什么我要使用@rel_user而不是@current_user?我测试过他们,他们都似乎工作。 –

相关问题