我在rails中有一个简单的form_for,单击“add”后不会创建新记录。我有一个叫做机会的模型,它有一个名为Contact的嵌套资源。联系人的表单是我遇到的问题。我将我的表单与可行的类似嵌套资源进行了比较,但无法找到问题。这里是我的代码:form_for not working rails 4.0
联系控制器:
def new
@contact = Contact.new
end
def create
@opportunity = Opportunity.find(params[:opportunity_id])
@contact = @opportunity.contacts.new(contact_params)
if @contact.save
redirect_to @opportunity, notice: 'Contact has been added'
else
redirect_to @opportunity, alert: 'Unable to add Contact'
end
end
def contact_params
params.require(:contact).permit(:first_name)
end
这里要说的是我有问题的具体形式:
的意见/联系人/ new.html.erb:
<div id="new_contact_form">
<%= form_for ([@opportunity, @opportunity.contacts.new]) do |f| %>
<div class="field">
<%= f.label "Contact Info:" %> <br />
<%= f.text_area :first_name %>
</div>
<div class="actions">
<%= f.submit 'Add' %>
</div>
<% end %>
</div>
请帮忙...谢谢!!!
你需要在你的'new'行动来定义'@ opportunity'实例变量,这是什么使你的'new.html.erb'。 – vee 2014-09-04 17:17:27