您好我正在尝试创建一个表单,同时创建一个列表并将产品与其关联。
问题是表格不断升高
参数数量错误(0 代表 1)
提取的源代码(第 10 行附近):
7: <%= f.text_area :description, placeholder:
8: "Compose a description for it ..." %>
9: </div>
10: <%= l.fields_for :products do |builder| %>
11: <%= render 'shared/product_form', :l => builder %>
12: <% end %>
13: <%= l.submit "Create", class: "btn btn-large btn-primary" %>
应用程序跟踪是
app/views/shared/_list_form.html.erb:10:in `block in _app_views_shared__list_form_html_erb__184644094_33330696'
app/views/shared/_list_form.html.erb:1:in `_app_views_shared__list_form_html_erb__184644094_33330696'
app/views/lists/new.html.erb:7:in `_app_views_lists_new_html_erb__973495114_33282228'
代码如下:
- -看法 - -
--list_form--
<%= form_for(@list) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<div class="field">
<%= f.text_field :name, placeholder:
"Come up with a name for your list" %>
<%= f.text_area :description, placeholder:
"Compose a description for it ..." %>
</div>
<%= f.fields_for :products do |builder| %>
<%= render 'shared/product_form', :f => builder %>
<% end %>
<%= f.submit "Create", class: "btn btn-large btn-primary" %>
<% end %>
--product_form--
<%= f.text_field :name, "Name:" %>
<%= f.text_area :description, :rows => 3 %>
- -模型 - -
- 列表 -
class List < ActiveRecord::Base
attr_accessible :description, :name
belongs_to :user
has_many :products, :dependent => :destroy
accepts_nested_attributes_for :products, :reject_if => lambda { |a| a[:name].blank? }, :allow_destroy => true
has_many :list_categorization
has_many :category, :through => :list_categorization
validates :user_id, presence: true
validates :name, presence: true, length: {maximum: 10}
validates :description, length: {maximum: 140}
default_scope order: 'lists.created_at DESC'
def categorize!(category_id)
list_categorization.create!(category_id: category_id)
end
end
- 产品 -
class Product < ActiveRecord::Base
attr_accessible :description, :donated, :name
validates :list_id, presence: true
belongs_to :list
end
---控制器--- --list_controller--
def new
@list = List.new
@products = @list.products.build
end
def create
@list = current_user.lists.build(params[:list]) if signed_in?
if @list.save
flash[:success] ="List " + @list.name + "created!"
render 'new'
end
--product_controller--
def new
@product = Product.new
end
def create
@product = @product.build(params[:product]) if signed_in?
if @product.save
flash[:success] ="Product " + @product.name + "created!"
end
你是对的,我在发布后实际上意识到了这一点,但现在在尝试提交表单时发生了这种情况:
表单包含 1 个错误。* 姓名不能为空
艰难的事件我正确填写了,这就是通过的
--- !ruby/hash:ActiveSupport::HashWithIndifferentAccess utf8: ✓authenticity_token: 38CXjVORlj2RBgoTetIMoHomcVgOIlBU5rW3NTgkRkU= list: !ruby/hash:ActiveSupport::HashWithIndifferentAccess name: list description: 这是一个列表 products_attributes: !ruby/hash:ActiveSupport::HashWithIndifferentAccess '0': !ruby/hash:ActiveSupport::HashWithIndifferentAccess 名称:p1 描述:这是一个产品提交:创建操作:创建控制器:列表