简体   繁体   English

嵌套表单:资源是动态添加的,但不会创建吗?

[英]Nested Form: Resource adds dynamically but doesnt get created?

I am using the nested form gem and i add products dynamically to the form. 我正在使用嵌套表单gem,并且将产品动态添加到表单中。 When i do click "add", another product resource appears but on creation it ERASES the former ones from being created entirely. 当我单击“添加”时,将出现另一个产品资源,但是在创建时会擦除以前的产品资源,而不会完全创建它们。 This is how the scenario goes: 场景是这样的:

  1. Fill in Location 填写位置
  2. Choose Date 选择日期
  3. Fill in Product ( one is already on form) 填写产品(一种已经在表格上)
  4. Add 5 more products (Products 2, 3, 4, 5) 再添加5个产品(产品2、3、4、5)
  5. Fill in All Products 填写所有产品
  6. "click" Create “点击”创建
  7. Created Product 5 创建的产品5

This is how my nested form looks: 这是我的嵌套表单的外观:

<%= nested_form_for @location, :url => products_path(@product) do |f| %>
    <%= f.label :business %>
    <%= f.text_field :business %>
    <%= f.label :address %>
    <%= f.text_field :address %>

 <%= f.fields_for :product_dates, :url => products_path(@product) do |d| %>
     <%= d.label :date %>
     <%= d.date_select :date %>

 <%= d.fields_for :products, :url => products_path(@product) do |p| %>
     <%= p.text_field :name %>
     <%= p.text_field :price %>
     <%= p.text_field :tag_list %>
     <%= p.link_to_remove "Remove Product" %>       
     <% end %>
     <%= d.link_to_add "Add", :products %>
 <% end %>
 <%= f.submit "Finish" %>
<% end %>

Controller: 控制器:

class ProductsController < ApplicationController

    def new
        @location = Location.new
        @product = Product.new
        product_date = @location.product_dates.build
        product_date.products.build
    end

    def create
        @location = Location.create(params[:location])
        if @location.save
            flash[:notice] = "Products Created."
            redirect_to :action => 'index' 
        else
            render :action => 'new'
        end
  end

Models: 楷模:

class User < ActiveRecord::Base
  devise
  attr_accessible :email, :password, :password_confirmation, :remember_me
  has_many :products,  :dependent => :destroy
end


class Location < ActiveRecord::Base
    attr_accessible :address, :business, :product_dates_attributes
    has_many :products
    has_many :product_dates 
    accepts_nested_attributes_for :product_dates    
end

class ProductDate < ActiveRecord::Base
    attr_accessible :date, :products_attributes
    belongs_to :location
    belongs_to :user
    has_many :products
    accepts_nested_attributes_for :products
end

class Product < ActiveRecord::Base
    attr_accessible :name, :price, :tag_list
    belongs_to :user
    belongs_to :location
    belongs_to :product_date
end

Any Suggestions? 有什么建议么?

First of all remove the url_for declarations on the fields_for declarations so you get 首先,删除fields_for声明中的url_for声明,以便您获得

<%= nested_form_for @location, :url => products_path(@product) do |f| %>
    <%= f.label :business %>
    <%= f.text_field :business %>
    <%= f.label :address %>
    <%= f.text_field :address %>

    <%= f.fields_for :product_dates do |d| %>
       <%= d.label :date %>
       <%= d.date_select :date %>

       <%= d.fields_for :products do |p| %>
         <%= p.text_field :name %>
         <%= p.text_field :price %>
         <%= p.text_field :tag_list %>
         <%= p.link_to_remove "Remove Product" %>       
       <% end %>
       <%= d.link_to_add "Add", :products %>
    <% end %>
 <%= f.submit "Finish" %>
<% end %>

What is really confusing is your whole routing and params approach. 真正令人困惑的是您的整个路由和参数方法。 It's just not right. 只是不对。 You have a form_for @location with a :url products_path(@product) This will right royally cause issues with the params that are sent back and there in lies the problem. 您有一个带有:url products_path(@product)的form_for @location,这将正确地导致发送回参数的问题,并且存在问题所在。 Stick with routing to location controller not the products controller by removing the products_path(@product) form your nested_form_for declaration and you will find that you will have all the necessary records saved but you will most likely need to change the redirect_to declaration in the locations_controller create action and the same for the update_action. 通过从您的nested_form_for声明中删除products_path(@product)来坚持路由到位置控制器而不是产品控制器,您会发现您已保存了所有必要的记录,但是您很可能需要在locations_controller创建中更改redirect_to声明操作与update_action相同。

But why use the products controller at all when you are dealing with a location? 但是,为什么在处理位置时完全使用产品控制器? Again this just isn't natural or intuitive. 再次,这只是不自然或直观的。

One last thing. 最后一件事。 Your remove links won't work as you have not added the necessary :dependent => :destroy declaration to the has_many declarations and you are also missing the :reject_if procs and the :allow_destroy => true declarations on the accepts_nested_attributes declarations. 您的删除链接无效,因为您没有在has_many声明中添加必要的:depend =>:destroy声明,并且在accepts_nested_attributes声明中也缺少:reject_if procs和:allow_destroy => true声明。

Can I strongly suggest that you 1) Use either the locations controller or the products controller not both I mean link to get to this form link_to the locations controller and set everything up there or use form_for @product rather than @location and handle everything in the products controller 我可以强烈建议您1)不能同时使用locations控制器或products控制器,我的意思是链接获取此表单link_to locations控制器并在那里设置所有内容,或使用form_for @product而不是@location并处理产品负责人

2) watch the railscasts that this gem is based on very closely http://railscasts.com/episodes/196-nested-model-form-part-1 http://railscasts.com/episodes/197-nested-model-form-part-2 2)密切注意该宝石基于非常严格的railscasts http://railscasts.com/episodes/196-nested-model-form-part-1 http://railscasts.com/episodes/197-nested-model-第二部分

3) Spend some time learning about how rails form view helpers arrange for the params hash to be organised in the controllers actions. 3)花一些时间来学习Rails窗体视图助手如何安排在控制器动作中组织params哈希。 In your case, have a close look at your log file for the parameters that come into the create action as things currently stand. 就您而言,请仔细查看您的日志文件,以了解当前情况下create动作中所包含的参数。 You will most likely see that the params are not nested as you would exect them to be which is why the nested attributes declaration is not behaving as expected 您很可能会看到参数没有嵌套,正如您将其说明的那样,这就是为什么嵌套属性声明的行为不符合预期的原因

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM