English 中文(简体)
未提交任何文件
原标题:Nested model form not submitting everything

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."

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.

www.un.org/Depts/DGACM/index_spanish.htm 什么是: 下表是服务供应商和商业开发商。 最后,我将增加联系人、照片和档案,但我认为我会开始简单地工作。

<<>Models>:

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

<>9>

<%= 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”两项建议外,还需要加以改变。

<><>

我再次查看了RailsCasts #196,并注意到我的表格与它所用的表格不同,因此我试图对:

    <%= 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%>

Initially, this presented an error:

undefined method `service_offerings_path 

www.un.org/Depts/DGACM/index_french.htm

这导致我了解RESTful Lines,因为我使用旧的路轮:

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

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

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

<<>strong>attr_accessible:

这形式是显而易见的,但还没有奏效。 因此,我在该网站上进行了一些搜索,并找到了号<该<<<>号>,其中谈到在你的母物体模型中添加“thing_attributes”的内容。 因此我这样做了:

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的建议以及下文所列控制器解决了这一变化。

def new
 @service_offering = ServiceOffering.new
 @business_developer = @service_offering.business_developers.build
end 
问题回答

你只是想分配@business_developper。

def new
  @service_offering = ServiceOffering.new
  @business_developper = @service_offering.business_developpers.build
end

-

@business_developer = @service_offering.business_developers.build

initializes an instance of biz_dev which is then available in the view. fields_for :biz_dev isn t really tied to this instance but to the many-to-many relationship btw serv_off and biz_dev.

这样,如果你在你的控制员中启动另一个双子公司,你就可以为更多的双子公司提供多种投入。 例如:

5.times { @service_offering.biz_dev.build }

在你看来,如果你不需要宣布,就将增加你形式的其他领域。

我希望它能够提供帮助。





相关问题
rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

SSL slowness in EC2

We ve deployed our rails app to EC2. In our setup, we have two proxies on small instances behind round-robin DNS. These run nginx load balancers for a dynamically growing and shrinking farm of web ...

Auth-code with A-Za-z0-9 to use in an URL parameter

As part of a web application I need an auth-code to pass as a URL parameter. I am currently using (in Rails) : Digest::SHA1.hexdigest((object_id + rand(255)).to_s) Which provides long strings like : ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

activerecord has_many :through find with one sql call

I have a these 3 models: class User < ActiveRecord::Base has_many :permissions, :dependent => :destroy has_many :roles, :through => :permissions end class Permission < ActiveRecord::...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签