[英]Nested Form: Resource adds dynamically but doesnt get created?
我正在使用嵌套表单gem,并且将产品动态添加到表单中。 当我单击“添加”时,将出现另一个产品资源,但是在创建时会擦除以前的产品资源,而不会完全创建它们。 场景是这样的:
这是我的嵌套表单的外观:
<%= 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 %>
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
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
有什么建议么?
首先,删除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 %>
真正令人困惑的是您的整个路由和参数方法。 只是不对。 您有一个带有:url products_path(@product)的form_for @location,这将正确地导致发送回参数的问题,并且存在问题所在。 通过从您的nested_form_for声明中删除products_path(@product)来坚持路由到位置控制器而不是产品控制器,您会发现您已保存了所有必要的记录,但是您很可能需要在locations_controller创建中更改redirect_to声明操作与update_action相同。
但是,为什么在处理位置时完全使用产品控制器? 再次,这只是不自然或直观的。
最后一件事。 您的删除链接无效,因为您没有在has_many声明中添加必要的:depend =>:destroy声明,并且在accepts_nested_attributes声明中也缺少:reject_if procs和:allow_destroy => true声明。
我可以强烈建议您1)不能同时使用locations控制器或products控制器,我的意思是链接获取此表单link_to locations控制器并在那里设置所有内容,或使用form_for @product而不是@location并处理产品负责人
2)密切注意该宝石基于非常严格的railscasts http://railscasts.com/episodes/196-nested-model-form-part-1 http://railscasts.com/episodes/197-nested-model-第二部分
3)花一些时间来学习Rails窗体视图助手如何安排在控制器动作中组织params哈希。 就您而言,请仔细查看您的日志文件,以了解当前情况下create动作中所包含的参数。 您很可能会看到参数没有嵌套,正如您将其说明的那样,这就是为什么嵌套属性声明的行为不符合预期的原因
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.