[英]RoR wrong number of arguments (0 for 1) form
嗨,我正在嘗試創建一個表單,同時創建一個列表並將產品與之關聯。
問題在於表格不斷提高
參數數量錯誤(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名稱:列表描述/由: '0':!ruby / hash:ActiveSupport :: HashWithIndifferentAccess名稱:p1說明:這是產品提交:創建操作:創建控制器:列表
l
從哪里來的? 我很確定您需要將其更改為f
:
<%= 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', :l => builder %>
<% end %>
<%= f.submit "Create", class: "btn btn-large btn-primary" %>
<% end %>
您的代碼有一些問題。 首先,如果您@list = current_user.lists.build(params[:list]) if signed_in?
這意味着,如果沒有用戶登錄,則完全不會創建該對象。 進行此類操作的正確方法是在控制器中使用before_filter
。
其次@product = @product.build(params[:product])
將不起作用。 您尚未初始化Product
對象,並且尚未將其分配給@product
。 build
也用於關聯。 您需要將其更改為@product = Product.new(params[:product])
。
列出控制器:
before_filter :user_signed_in? # add to products controller as well
# if you need this filter only on certain actions then do:
# before_filter :user_signed_in?, only: [:new, :create]
def new
@list = current_user.lists.build
@products = @list.products.build
end
def create
@list = current_user.lists.build(params[:list])
if @list.save
flash[:success] = "List " + @list.name + " created!"
redirect_to lists_path # this part was missing!
else # this was also missing
render 'new'
end # you had an 'if' with no 'end'
end
private
# add the following to Products controller as well, or if you
# use it a lot then place it in your application controller
def user_signed_in?
unless signed_in?
flash[:notice] = "You must first sign in"
redirect_to sign_in_path
end
end
產品控制器:
def new
@product = Product.new
end
def create
@product = Product.new(params[:product]
if @product.save
flash[:success] = "Product " + @product.name + " created!"
redirect_to @product
else
render 'new'
end
end
據我記得,通過嵌套表單保存產品時不會使用products#create
操作,lists#create操作將同時用於這兩種方法。
要了解有關嵌套表單的更多信息,請查看這些railscasts 。
更新代碼並瀏覽完這些視頻后,如果您仍然遇到錯誤,我建議創建一個新問題,因為這個問題已經變得冗長而混亂了:)
您忘記這樣做:
rails generate migration add_remember_token_to_users
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.