我正在尝试从帐户对象的“显示”页面创建一个新的联系人对象。我知道下面的代码不正确。如果我在帐户的“显示”页面上,如何将该帐户 ID 传递给新联系人表单,以便创建属于该帐户的新联系人?
联系belongs_to账户
帐户has_many联系人
帐户“显示”视图,其中我有指向新联系人的链接
<%= link_to "Add Contact", new_account_contact_path(@account), class: 'btn' %>
使用建议的编辑“新建、创建”操作联系控制器
class ContactsController < ApplicationController
before_filter :authenticate_user!
before_filter :load_account
respond_to :html, :json
...
def create
@contact = @account.contacts.new(params[:contact])
if @contact.save
redirect_to account_path(params[:account]), notice: "Successfully created Contact."
else
render :new
end
end
def new
@contact = @account.contacts.new
end
...
end
新的联系表
<%= simple_form_for(@contact) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<%= f.input :firstname %>
<%= f.input :lastname %>
<%= f.input :email %>
<%= f.input :phone %>
<%= f.input :note %>
</div>
<div class="form-actions">
<%= f.button :submit %>
</div>
<% end %>
错误
undefined method `contacts_path' for #<#<Class:0x007f86c0c408d0>:0x007f86c0be7488>
Extracted source (around line #1):
1: <%= simple_form_for(@contact) do |f| %>
2: <%= f.error_notification %>
3:
4: <div class="form-inputs">