嵌套模型表单未提交所有内容 [英] Nested model form not submitting everything

查看:38
本文介绍了嵌套模型表单未提交所有内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在听 Hypercritical 的时候听说了这个社区,我很高兴加入我的第一个问题.我正在开发我的第一个 rails 应用程序,但遇到了一个我似乎无法破解的问题.我一直在看 Railscast、Lynda.com 和谷歌搜索,但我仍然无法理解如何创建一个表单来立即更新我的 has_many :through 关联.请允许我尝试解释一下我在做什么.

I heard about this community while listening to Hypercritical and I am excited to join in with my first question. I am working on my first rails App and I have run into an issue that I cannot seem to crack. I have been watching Railscast, Lynda.com, and Googling for days but I still cannot comprehend how to create a form that that will update my has_many :through associations at once. Allow me to try an explain what I am doing.

我的目标:我工作的公司提供了许多服务产品",我希望能够在一页上创建新的服务产品,并让它创建与之相关的联系人和其他信息.诸如联系人"之类的附加信息将存在于它们自己的表中,因为它们可能需要被许多服务产品"引用.

My Goal: The firm I work for provides many "Service Offerings" and I want to be able to create a new service offering on one page and have it create the contacts and other information that is associated with it. The additional information such as "contacts" will live in their own tables because they may need to be referenced by many "Service Offerings."

问题:当我提交表单时,服务产品"字段提交并输入到数据库中,但业务开发人员"的字段没有.显然,我希望将所有内容都输入到相应的表中,并将 ID 链接到连接表中.我真的很感激你能提供的任何见解.

Problem: When I submit the form the "Service Offering" fields submit and are entered into the database, but the fields for the "Business Developer" do not. Obviously, I would like everything to be entered into its appropriate table and the for the IDs to be linked in the join table. I would really appreciate any insight that you could provide.

我目前所拥有的:您在下面看到的是服务产品和业务开发人员.最终我会添加联系人、照片和文件,但我想我会简单地开始并逐步增加.

What I Have So Far: What you see below is Service Offerings and Business Developers. Eventually I will be adding Contacts, Photos, and Files but I thought I would start simply and work my way up.

模型:

class ServiceOffering < ActiveRecord::Base
 attr_accessible :name, :description
 has_many :business_developer_service_offerings
 has_many :business_developers, :through => :business_developer_service_offerings
 accepts_nested_attributes_for :business_developer_service_offerings
end

class BusinessDeveloper < ActiveRecord::Base
 attr_accessible :first_name, :last_name
 has_many :business_developer_service_offerings
 has_many :service_offerings, :through => :business_developer_service_offerings
end

class BusinessDeveloperServiceOffering < ActiveRecord::Base
 belongs_to :business_developer
 belongs_to :service_offering
end

控制器:

def new
 @service_offering = ServiceOffering.new
 @service_offering.business_developers.build
end

def create
 @service_offering = ServiceOffering.new(params[:service_offering])
 if @service_offering.save
  redirect_to(:action => 'list')
 else
  render('new')
 end
end

查看:

<%= form_for((@service_offering), :url => {:action => 'create'}) do |f|%>
    <p>
    <%= f.label :name%>             
    <%= f.text_field :name %>

    <%= f.label :description%>  
    <%= f.text_field :description %>
    </p>

<%= f.fields_for :business_developer do |builder| %>
    <p>
    <%= builder.label :first_name%>
    <%= builder.text_field :first_name %>

    <%= builder.label :last_name%>
    <%= builder.text_field :last_name %>
    </p>
<%end%>
    <%= f.submit "Submit" %>
<%end%>

推荐答案

我想通了.事实证明,除了@Delba 提出的两个建议之外,还有一些地方是错误的,需要更改.

I figured it out. It turns out a few things were wrong and needed to be changed in addition to the two suggestions @Delba made.

表格:

我再次查看了 RailsCasts #196并注意到我的表格看起来与那里使用的表格不同,所以我尝试将其匹配:

I took a look at RailsCasts #196 again and noticed that my form looked different than the one used there, so I tried to match it up:

    <%= form_for @service_offering do |f|%>
     <p>
      <%= f.label :name%>             
      <%= f.text_field :name %>

      <%= f.label :description %>  
      <%= f.text_field :description %>
     </p>
    <%= f.fields_for :business_developers do |builder| %>
     <p>
      <%= builder.label :first_name %>
      <%= builder.text_field :first_name %>

      <%= builder.label :last_name %>
      <%= builder.text_field :last_name %>
     </p>
<%end%>
    <%= f.submit "Submit" %>
<%end%>

最初,这是一个错误:

undefined method `service_offerings_path'

路线:

这让我了解了 RESTful Routes 因为我使用的是旧的路由风格:

This lead me to learn about RESTful Routes because I was using the old routing style:

match ':controller(/:action(/:id(.:format)))'

所以我将我的路由更新为新的 RESTful 路由样式:

So I updated my routes to the new RESTful Routes style:

  get "service_offerings/list"
  resource :service_offerings
  resource :business_developers

attr_accessible:

这使表单可见,但仍然无法正常工作.所以我在这个网站上做了一些搜索,发现 这篇文章谈到在 attr_accessible 下将something_attributes"添加到您的父对象模型中.所以我做到了:

That got the form visible but it was still not working. So I did some searching around on this site and found this post that talked about adding "something_attributes" to your parent objects model under attr_accessible. So I did:

class ServiceOffering < ActiveRecord::Base 
 has_many :business_developer_service_offerings
 has_many :business_developers, :through => :business_developer_service_offerings
 accepts_nested_attributes_for :business_developers
 attr_accessible :name, :description, :business_developers_attributes  
end

上述模型和控制器中显示的更改以及@Delba 的建议解决了这个问题.

That change along with @Delba's suggestion shown in the above model and controller listed below solved it.

def new
 @service_offering = ServiceOffering.new
 @business_developer = @service_offering.business_developers.build
end 

这篇关于嵌套模型表单未提交所有内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆