我有以下内容:
路线:
.
.
.
resources :users do
resources :relationships
end
新的.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>
关系 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>
有什么想法吗?
最佳答案
你应该明白所有的'_path'助手都是从route.rb文件生成的。因此,在您的情况下,route 将生成此助手 users_relationship_path 以显示操作。
但是在您的表单中,您只使用 form_for @relationship,它应该使用 relationship_path 助手。
所以你应该告诉你的表单助手使用嵌套,像这样:
<%= form_for [@rel_user, @relationship] do |f| %>
关于ruby-on-rails - Rails 3.1 中的未定义方法 `relationships_path',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7933415/